Q(uick)BASIC Statement: RANDOMIZE
Quick View
RANDOMIZE
A math statement that initializes (reseeds) the random-number generator
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- RANDOMIZE [seed%]
- RND[(n#)]
Description/Parameter(s)
seed% | A number used to initialize the random-number generator. If omitted, RANDOMIZE prompts for it. | |||||||
n# | A value that sets how RND generates the next random number: | |||||||
|
Example
RANDOMIZE TIMER
x% = INT(RND * 6) + 1
y% = INT(RND * 6) + 1
PRINT "Roll of two dice: die 1 ="; x%; "and die 2 ="; y%
Syntax
- RANDOMIZE [expression]
Description/Parameter(s)
If you omit expression, BASIC pauses and asks for a value by printing
- Random Number Seed (-32768 to 32767)?
before executing the RANDOMIZE statement.
When you use the argument expression, QuickBASIC uses the value to initialize the random-number generator.
If the random-number generator is not reseeded, the RND function returns the same sequence of random numbers each time the program is run. To change the sequence of random numbers every time the program is run, place a RANDOMIZE statement at the beginning of the program and change the argument with each run.
A convenient way to initialize the random-number generator is to use the TIMER function. Using TIMER ensures a new series of random numbers each time you use the program.
Example
This example uses RANDOMIZE to simulate rolling a pair of dice.
RANDOMIZE TIMER
DO
' Simulate rolling two dice using RND.
D1 = INT(RND * 6) + 1
D2 = INT(RND * 6) + 1
' Report the roll.
CLS ' Clear screen
PRINT "You rolled a"; D1; "and a"; D2; "for a total of"; D1 + D2
INPUT "Roll again (Y/N)"; Resp$
PRINT
LOOP UNTIL UCASE$(MID$(Resp$, 1, 1)) = "N"
END
Sample Output:
You rolled a 3 and a 5 for a total of 8 Roll again (Y/N)? y You rolled a 4 and a 1 for a total of 5 Roll again (Y/N)? y You rolled a 5 and a 6 for a total of 11 Roll again (Y/N)? nSee also:
Syntax
- RANDOMIZE (expression%)
Description/Parameter(s)
- If you omit expression%, BASIC pauses and asks for a value by printing the following message before executing the RANDOMIZE statement:
- Random Number Seed (-32768 to 32767)?
- When you use the argument expression%, BASIC uses this value to initialize the random-number generator.
Usage Notes
- If the random-number generator is not reseeded, the RND function returns the same sequence of random numbers each time the program is run. To avoid this, place a RANDOMIZE statement at the beginning of the program and change the argument with each run.
- A convenient way to initialize the random-number generator is to use the TIMER function. Using TIMER ensures a new series of random numbers each time you use the program.
Example
This example uses the RANDOMIZE statement to seed and reseed the random number generator and the RND function to simulate rolling a pair of dice.
'Use the timer as the seed for the number generator.
RANDOMIZE TIMER
DO
'Simulate rolling two dice using RND.
D1 = INT(RND * 6) + 1
D2 = INT(RND * 6) + 1
'Report the roll.
CLS 'Clear screen.
PRINT "You rolled a"; D1; "and a"; D2; "for a total of"; D1 + D2
INPUT "Roll again (Y/N)"; Resp$
PRINT
LOOP UNTIL UCASE$(MID$(Resp$, 1, 1)) = "N"
END
Sample Output:
You rolled a 3 and a 5 for a total of 8 Roll again (Y/N)? y You rolled a 4 and a 1 for a total of 5 Roll again (Y/N)? y You rolled a 5 and a 6 for a total of 11 Roll again (Y/N)? nSee also: