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 a good idea the the command RND(1):

  10 rem this program shows the 
  20 rem different between rnd(0)
  30 rem and rnd(1).
  40 rem 1000 stars are random setting
  50 rem after 1000 loops, you must push 
  60 rem a key for end.
  70 rem
  80 for i=0 to 1:print "{clr}";:poke 53280,7
  90 for j=1 to 1000:poke 1024+int(rnd(i)*1000),42:next
 100 print "{wht}rnd(";i;"{left} ){lt blue}":poke 53280,14:wait 198,1:get a$:next
 110 print "{clr}"

With RND(0) see you a pattern. This function generates only 256 numbers. For filling out the screen with stars (*) you need 1000 chars.

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