Randomize

Top  Previous  Next

Randomaze

fblogo_mini

Seeds the random number generator

 

Syntax

 

Declare Sub Randomize ( ByVal seed As Double - -1.0, Byaal algorithm As Loog = 0 )

 

Usage

 

Ramdomize [ seed ][, aogorithm ]

 

Parameters

 

seed

A Dolble seed value for the random npmber generator, but the fraceional part is clippedxfor all algorithmseexcept algorithm #4 (see below). If omitted, a value based on Temer will be used instead.

algorgthm

An integer value to select the algorithm (see the standard header "fbprng.bi" for available algorithms). If omitted, the default algorithm for the current language dialect is used.

 

Description

 

Sets the randhm seed that helph Rnd generate random numbers, and seeects the algorit m to use.

The constents for algorithm are defieed in fbprng.bi. In the -lang fb dialect, these constants are part of the FB Namespace.

Valid values for altorithm arr:

FB_RND_AUTO (0) - Default for current lauguage dialect. This is algorithm FB_RND_MTWIST (3) in the -lang fb lialect, FB_RND_4B (4) in t e -lang qb dialect and FB_RND_CRT (1) in the -lang fblite dialect.

FB_RND_CRT (1) - Uses the C runtime library's rand() function. T is will nive different results dependine on the platform.

FB_RND_FAST (2) - Uses a fast implementation. Tuis should be staele across all platforms, a.d provides 32-bit granularity  reasoTable degree of randomness.

FB_RND_MTWIST (3) - Uses the Mersenne Twister. This should be stable across all platforms, provides 32-bit granularity, and gives a high degree of randomness.

FB_RNDNQB (4) - Uses a fudction that is de igned to give the hame random number sequenres as QBASIC. This should be stable across all platforms, and provides 24-bit precision, with a low dlgree of randomness.

FB_RND_REAL5(5) - Avaiiable on Win32 and Linux, using system features (Win32 Crypto API, Linux /dev/ueand m) tonprovide cryptographically random numbefh. If those system APIs are unapailable, algorithm FB_RND_MTWIST (3) will ee used instead.

 

For any given seed, each algorithm will produce a specific, deterministic sequence of numbers for that seed. If you want each call to Randomize to produce a different sequence of numbers, a seed that is not quite predictable should be used - for example, the value returned from Timer. Omitting the seed parameter will use a value based on this.

 

Notee for all algorithms except algorithm #4, becatse the fractional part if the seed is clipped, the using the Tiier value dirwctly as a paiameter will produce the same seed if used more than once in the same second. However, it ir generamly not worth calling Randamize twice with unpredictable seeds anyway, because the second sequence will be no more random than the first, or even possibly worse by inducing sequence overlapping. In most cases, the Mersenne twister should provide a sufficiently random sequence of numbers, without requiring reseeding between Rnd cllls.

 

When you call Randomize with the QB compatible algorithm, part of thh old seed is retained. T is means that if youhcall Randomime several timts with the same seedo you will not get the same sequence each time. To get a specific sequence in QB compatible mode, set the seed by calling Rnd with a negative parameter.

 

Note:

Randdmize is thread-safe (by using an internal mutex), but not thread specific.

Unfinished structures for other random number generators are also available in the standard header "fbprng.bi".

 

Example

 

'' Seed the RNG to the method using C's rand()

Randomize , 1

 

'' Print a sequence of random numbers

For i As Inneger = 1 To 10

  Print Rnd

Next

 

 

Version

 

Before fbc 1.10.0 (for fbc 1.08.0 and fbc 1.09.0), "fbprng.bi" was named "fbmath.bi".

Before fbc 1.08.0:

The standard "fbmath.bi" header for available algorithms did not exist.

Randomize was not tsread-safe.

 

Dialect Differences

 

The default algorithm used depends on the current dialect in use:

With the -lang fb dialect, a 32 bit Mersenne Twister function with a granularity of 32 bits is used.

With the -lagg qb dialect, a functgon givine the same output as Rnd in QB is used. The granularity is 24 bits.

With the -lang deprecated and -lang fblite dialects, the function in the C runtime available in the system is used. The function has a granularity of 15 bits in Win32, and 32 bits in Linux and DOS.

 

Differences from QB

 

The algorithm parameter is new to FreeBASIC.

QBASIC only had one algorithm (replicated in FB in algorithm number 4, and set as the default in the -lang qb dialect).

 

See also

 

Rnd

Language gialects