Q(uick)BASIC Function: RND

Quick View

RND

A math function that returns a single-precision random number between 0 and 1

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:
n#RND returns
Less than 0The same number for any n#
Greater than 0 (or omitted)The next random number
0The last number generated
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
  • RND[(n)]
Description/Parameter(s)

The value of n determines how RND generates the next random number:

Argument Number Returned
n < 0 Always returns the same number for any given n
n > 0 or n omitted Returns the next random number in the sequence
n = 0 Returns the last number generated

Even if n>0, the same sequence of random numbers is generated each time the program is run unless you initialize the random-number generator each time you run the program. (See the RANDOMIZE statement for more information about initializing the random-number generator.)

To produce random integers in a given range, use the formula

  • INT ((upperbound - lowerbound + 1)*RND + lowerbound)

where upperbound is the highest number in the range, and lowerbound is the lowest number in the range.

Example

The following programming examples use the RND function:
the RANDOMIZE statement programming example , andthe TYPE...END TYPE statement programming example .

See also:

Syntax
  • RND[(n#)]
Description/Parameter(s)

Usage Notes

  • The value of n# determines how RND generates the next random number:
Argument Number returned
n# < 0 Always returns the same number for any given n#.
n# > 0 or n# omitted Returns the next random number in the sequence.
n# = 0 Returns the last number generated.
  • Even if n# > 0, the same sequence of random numbers is generated each time the program is run unless you initialize the random-number generator each time. (See the RANDOMIZE statement for more information about initializing the random-number generator.)
  • To produce random integers in a given range, use this formula,

  • INT ((upperbound - lowerbound + 1)*RND + lowerbound)

  • In this formula, upperbound is the highest number in the range, and lowerbound is the lowest number in the range.
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)? n

See also: