RND

From C64-Wiki
Jump to: navigation, search
BASIC keyword
Keyword: RND
Abbreviation: R, Shift+N
Type: Function
Token code: 187/$BB
Handling routine
in BASIC ROM:
57495–57592
$E097–E0F8
List of all BASIC keywords


Remark: This article describes the BASIC-Command RND in BASIC V2 at the Commodore 64.

Typ: Function 
General Programming-Syntax: RND(<Number>)

The function RND generates random floating point numbers in the range of 0.0 (inclusive) to 1.0 (exclusive). The argument <number> can be positive, negative or zero.

  • By using RND(<positive number>) gives a different random number each time from a predetermined sequence (the sequence number is stored internally).
  • Using RND(<negative number>) jumps to a point in the sequence determined by the particular negative number used. Repeatedly calling RND with the same negative number results in the same result; typical use is to call RND(<negative number>) once and then repeatedly call RND(<positive number>).
  • With RND(0) the C64 generates the random number from the internal clock, though the possible values are limited as the digits of the clock only fall in the range 0-60, so it is not suitable for generating large ranges.

By using a invalid numeric value the BASIC error ?TYPE MISMATCH ERROR appears. When the numeric argument is absent, it appears as ?SYNTAX ERROR.

Examples[edit]

X = RND(-TI)           : REM Initialising
PRINT INT(RND(1)*100)  : REM Integer random numbers from 0 to 99
PRINT INT(RND(1)*6)+1  : REM Integer random numbers from 1 to 6 (for dice simulation)
PRINT INT(RND(1)*49)+1 : REM Integer random number from 1 to 49 (for lotto simulation, for example famous German lotto 6 out 49)
PRINT (RND(0)*101)+100 : REM Random numbers from 100 to 201


Using the real-time clock for initialising the RND-function.

POKE 56328,0    : REM real-time clock activate with 1/10s -> clock runs ($DC08)
PRINT RND(0)    : REM Random number addicted by timer and real-time clock


This example shows why the command RND(0) isn't very good, when compared to the command RND(1):

10 REM THIS PROGRAM SHOWS THE DIFFERENCE BETWEEN RND(0) AND RND(1).
20 REM 1000 ASTERISKS WILL BE GENERATED FOR RND(0), 
30 REM AFTER THIS HAS FINISHED PRESS ANY KEY TO START RND(1).
40 FOR I=0 TO 1
50 PRINT CHR$(147): REM CLEARS SCREEN
60 POKE 53280,0: REM SETS BORDER COLOUR
70 FOR J=1 TO 1000
80 POKE 1024+INT(RND(I)*1000),42: NEXT
90 PRINT "{WHITE}{HOME}RND(";I;"){LT. BLUE}": REM {LT. BLUE}=LIGHT BLUE
100 POKE 53280,14: REM "RESETS" BORDER COLOUR
110 WAIT 198,1: REM WAITS UNTIL A KEY IS PRESSED
120 GET A$: NEXT
130 PRINT CHR$(147)

With RND(0) you see a pattern. This is because RND(0) can only generate 256 numbers. For filling out the screen with asterisks (*) you need 1000 characters, but do note that this code does not look whether a spot is filled with an asterisk or not, so it may write an asterisk where there was already one, making it look like fewer asterisks were written.

Basic RND 0.png Basic RND 1.png


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π  |POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT