ReadyBASIC
BASIC Language Reference Manual > Command and Function Reference >

RND Function

Summary

Returns a randomly generated floating-point number between 0 and 1.

Syntax

RND

Remarks and Examples

The RND function returns a fractional random number that falls within the range of 0 and 1.

Most commonly, RND is used for generating a random integer between a range of values. To generate a random integer number that is between the range of integers A (the lower limit) and B (the upper limit), inclusive, use the following pattern:

INT(RND * B + A)

For example, to generate a random number between 50 and 100, use INT(RND * 100 + 50).

The following example program picks six lottery numbers between 1 and 49.

10 PRINT "Your lottery numbers are: ";
20 FOR X = 1 TO 6
30 PRINT INT(RND * 49 + 1); " ";
40 NEXT X
RUN
Your lottery numbers are: 17 20 3 44 15 29
RUN
Your lottery numbers are: 8 46 19 11 21 37

(Beware that this program may pick the same number more than once in each set. A real lottery number program would have to guard against duplicates.)

Note: In some dialects of BASIC, the same sequence of random numbers is generated every time a program is run, unless the RANDOMIZE statement is explicitly executed. ReadyBASIC does not provide a RANDOMIZE statement, as ReadyBASIC automatically ensures that the random number generator is properly "reseeded" so that new random number sequences are generated with every new program execution. If you are typing in a BASIC program that includes a RANDOMIZE statement, you can omit the statement.




Copyright 2006-2008, Kevin Matz, All Rights Reserved.