RANDOMIZE statement

Purpose:

RANDOMIZE initializes the RND function random number generator.

Syntax:

RANDOMIZE([Number])

Parameters:

  • Data type: Scalar
    Number [OPTIONAL] A literal or variable number to be used as the seed for the RND function. If Number is not specified then TIMER is used as the seed for the RND function. Parentheses are optional.

Example:

The following code shows how to generate a cryptographically secure seed Number for the RANDOMIZE function.


 DIM AS DOUBLE Random
 DIM AS DOUBLE TheSeed
 DIM AS INTEGER i, j
 
 TheSeed = CryptoSeed()
 
 PUSHCOLORS
 
 CLS
 COLOR 2, 0 : LOCATE 12, 25, 0
 INPUT "Hit [Enter] to see a sample", i : COLOR 7, 0
 CLS
 RANDOMIZE(TheSeed)
 FOR i = 1 TO 23
   FOR j = 1 TO 70 STEP 20
     Random = RND
     LOCATE i, j, 0
     COLOR MOD(i, 4.0) + 10, 0
     PRINT Random
   NEXT
 NEXT
 LOCATE i, j
 COLOR 7, j
 
 FUNCTION CryptoSeed AS DOUBLE
 
   STATIC AS HCRYPTPROV hProv
   STATIC AS INTEGER isInit
   DIM AS UINT n
   DIM AS INTEGER ok
 
   IF isInit = 0 THEN
     ok = CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)
     IF ok = FALSE  THEN FUNCTION = -1
     isInit = 1
   END IF
 
   ' Cast &n to BYTE* so it works with CryptGenRandom 
   ok = CryptGenRandom(hProv, SIZEOF(n), (BYTE*)&n)
   IF ok = FALSE THEN FUNCTION = -1
 
   FUNCTION = CAST(DOUBLE, n / 4294967296.0)
 END FUNCTION
 
 POPCOLORS
 
 PAUSE
 

Remarks:

The above code can be considered FIPS-compliant when run on a properly configured system with a validated provider. On Windows, configuration can be enabled via Local Security Policy. For more information, visit the
Microsoft System cryptography: Use FIPS compliant algorithms for encryption, hashing, and signing webpage

BCX Console Sample Programs using the RANDOMIZE function.


RND function

Purpose:

RND returns, as a single precision float, a random value between 0.0 and 1.0.

Syntax:

RetVal = RND

Return Value:

  • Data type: SINGLE
    RetVal A random value between 0.0 and 1.0.

Parameters:

  • None.

Example:

RangeR.bas will emit a single precision float between the LowValue and HighValue entered on the command line.

' RangeR.bas
' Usage: RangeR LowValue HighValue RandomizerValue

DIM RetVal!
DIM hi!, lo!, rz!

lo! = VAL(COMMAND$(1))
hi! = VAL(COMMAND$(2))
rz! = VAL(COMMAND$(3))

IF rz! = 0.0 THEN
  RANDOMIZE
ELSE
  RANDOMIZE(rz!)
END IF

FOR INTEGER i = 1 TO 6
  RetVal! = RangeR(lo!, hi!)
  PRINT RetVal!
NEXT i

FUNCTION RangeR! (low!, high!)
 
  IF (low! > high!) THEN
    DIM t!
    t! = low!
    low! = high!
    high! = t!
  END IF
   
  FUNCTION = (high! - low! + 1) * RND + low!

END FUNCTION

Result:

C:\Dev\BCX>RangeR 9 27 13
21.05748
19.64941
9.756275
14.62314
17.23507
22.13492

BCX Console Sample Programs using the RND function.


RND2 function

Purpose:

RND2 returns, as a DOUBLE, a random value in a range between a specified lower and upper value. Lesser precision arguments, INTEGER, LONG, SINGLE are allowed.

Syntax:

RetVal = RND2(LowerNumber AS DOUBLE, UpperNumber AS DOUBLE)

Return Value:

  • Data type: DOUBLE
    RetVal A signed integer, a random value in a range between a specified lower and upper value.

Parameters:

  • Data type: DOUBLE
    LowerNumber The lower limit of the range of numbers.
  • Data type: DOUBLE
    UpperNumber The upper limit of the range of numbers.

Example:

$BCXVERSION "8.2.2"

DIM RetVal#

RANDOMIZE TIMER

FOR INTEGER i = 1 TO 6
  RetVal# = RND2(10.0, 11.0)
  PRINT RetVal#
NEXT i%

PAUSE

Result:

10.6598205566406
10.9359741210938
11
10.666015625
10.8933410644531
10.6361999511719

Press any key to continue . . .