Rnd
RandomInteger = Rnd(Range)
 
Parameters:

    Range = The Range value that the random number is selected within.
Returns:

    RandomInteger = The random number selcted by the RND function
 

      The Rnd function will return a random number between and zero and the range value selected. This range value and zero are inclusive, so their both possible outputs of the function. Each time you use the RND() function it will return a different result.

      While Rnd may appear to produce random results to us humans, the results it produces are in fact part of a 'set'. The size of these sets, will in the millions however, so it's unlikely you'll noticed the values repeating, but they do repeat. This is not a PlayBASIC issue, it's an issue with how random number generation techniques employed in computers.



FACTS:


      * The value returned by the Rnd function is inclusive of zero and the range limit.

      * See Randomize to seed the random number generator



EXAMPLES






Example #1: 10 Random Numbers


      Displaying a list of 10 random numbers between 0 and 100 (inclusive)

  
  For lp=1 To 10
   ; display a random number between 0 and 100
     Print Rnd(100)
  Next
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  



      This example could output something like this. (could as it'll be random set of results each time you run the program)

  
  66
  23
  77
  1
  99
  100
  3
  44
  17
  64
  


Top







Example #2: Using Randomize to seed RND generator


      In this example we're going to display a set of random numbers, but with one key difference to the above. This time by using the RANDOMIZE command to seed the random number geneator, we can be sure that set of randoms generated, will always be the same.

  
  
  
; Set the Random Number Generator Seed
  Randomize 546
  
  For lp=1 To 10
   ; display a random number between 0 and 100
     Print Rnd(100)
  Next
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  



      This example could output something like this. (could as it'll be random set of results each time you run the program)

  
  90
  58
  29
  44
  97
  44
  59
  50
  63
  34
  


Top







 
Related Info: Randomize | Rnd# | RndRange | RndRange# :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com