•  Back 
  •  Main 
  •  Index 
  •  Tree View 
  •  Cross references 
  •  Help 
  •  Show info about hypertext 
  •  View a new file 
Topic       : The GFA-Basic Compendium
Author      : GFA Systemtechnik GmbH
Version     : GFABasic.HYP v2.98 (12/31/2023)
Subject     : Documentation/Programming
Nodes       : 899
Index Size  : 28056
HCP-Version : 3
Compiled on : Atari
@charset    : atarist
@lang       : 
@default    : Document not found
@help       : Help
@options    : +g -i -s +z
@width      : 75
@hostname   : STRNGSRV
@hostname   : CAB     
@hostname   : HIGHWIRE
@hostname   : THING   
View Ref-FileRND[(x)]
RAND(y)
RANDOM(x)
RANDOMIZE [y]

x: aexp
y: iexp

This group of commands deal with the generation of random numbers:

RND() produces a random number between 0 and 1 (including 0, excluding 1). The
optional parameter (x) has no meaning.

RAND(y) produces a 16-bit random integer in the range 0 to y-1. Where y is an
integer with a maximum value of 65535 (&HFFFF). If y exceeds 65535, then its
low-order word is taken as the argument.

RANDOM(x) produces a random integer between 0 and x (including 0, excluding
x). The numerical expression x need not be integer if not all numbers are
required to have the same probability.

RANDOMIZE y initializes the random number generator with the value y. If the
random number generator is initialised several times with the same y, the same
sequence of random numbers will be produced. Without the use of RANDOMIZE at
the beginning of a program, different sequences will be generated each time the
program is run, which may be undesirable in some cases. For initialising the
random number generator one can also use RANDOMIZE (without parameters) or
RANDOMIZE 0, which initializes the random number generator with a random
number.

Examples:

    x=RND
    PRINT x,RND(2)

--> Two random numbers between 0 and 1 appear on the screen.

    x=RANDOM(2)
    y=RAND(4)
    PRINT x,y,RAND(x),RANDOM(3*x)

--> Four integer random numbers are displayed.

    RANDOMIZE 3
    x=RND
    RANDOMIZE 3
    PRINT x,RND

--> The same 'random number' is printed twice.

Memo: RAND() is integer based and thus faster than RANDOM().
      RANDOMIZE [0] seeds the random number generator with TIMER.
      RANDOM(x) is equivalent to TRUNC(RND*x).
      RANDOM() doesn't always produce the same number sequence when compiled.
      This fails:

        RANDOMIZE 123456
        FOR i=1 TO 3
          PRINT RANDOM(999)  !editor: 6, 677, 751  compiler: 602, 587, 525
        NEXT i

      Workaround if you require random numbers >65535:

        RANDOMIZE 123456
        FOR i=1 TO 3
          PRINT @random(9999)
        NEXT i
        FUNCTION random(num%) !fix
          $F%
          RETURN RANDOM(num%)
        ENDFUNC

      Reason for failure: Compiler tries to optimize by looking at the value
      passed and if it's <=65535 it calls RAND() instead, which breaks it.

      The MiNT kernel offers additional RNG options via:
        u:\dev\random and u:\dev\urandom
        From the MiNT kernel source file random.c:
        * This routine gathers environmental noise from device drivers, etc.,
        * and returns good random numbers, suitable for cryptographic use.
        * Besides the obvious cryptographic uses, these numbers are also good
        * for seeding TCP sequence numbers, and other places where it is
        * desirable to have numbers which are not only random, but hard to
        * predict by an attacker.

Random()+