#### Revision [21647]

This is an old revision of KeyPgRandomize made by fxm on 2016-10-30 18:58:57.

### RANDOMIZE

Seeds the random number generator

**Syntax:**

**Usage:**

`
`

**Randomize**[

*seed*][,

*algorithm*]

**Parameters:**

*seed*
A

`double`seed value for the random number generator. If omitted, a value based on`Timer`will be used instead.

*algorithm*
An integer value to select the algorithm. If omitted, the default algorithm for the current language dialect is used.

**Description:**

Sets the random seed that helps

For any given seed, each algorithm will produce a specific, deterministic sequence of numbers for that seed. If you want each call to

Note: using the

When you call

`Rnd`generate random numbers, and selects the algorithm to use. Valid values for`are:`*algorithm*`- Default for current language dialect. This is algorithm`

**0**`in the`

**3***-lang fb*dialect,

`in the`

**4***-lang qb*dialect and

`in the`

**1***-lang fblite*dialect.

`- Uses the C runtime library's`

**1**`rand()`function. This will give different results depending on the platform.

`- Uses a fast implementation. This should be stable across all platforms, and provides 32-bit granularity, reasonable degree of randomness.`

**2**`- Uses the Mersenne Twister. This should be stable across all platforms, provides 32-bit granularity, and gives a high degree of randomness.`

**3**`- Uses a function that is designed to give the same random number sequences as QBASIC. This should be stable across all platforms, and provides 24-bit precision, with a low degree of randomness.`

**4**`- Available on Win32 and Linux, using system features (Win32 Crypto API, Linux /dev/urandom) to provide cryptographically random numbers. If those system APIs are unavailable, algorithm`

**5**`will be used instead.`

**3**For any given seed, each algorithm will produce a specific, deterministic sequence of numbers for that seed. If you want each call to

`to produce a different sequence of numbers, a seed that is not quite predictable should be used - for example, the value returned from`

**Randomize**`Timer`. Omitting the

`parameter will use a value based on this.`

*seed*Note: using the

`Timer`value directly as a parameter will produce the same seed if used more than once in the same second. However, it is generally not worth calling

`twice with unpredictable seeds anyway, because the second sequence will be no more random than the first. In most cases, the Mersenne twister should provide a sufficiently random sequence of numbers, without requiring reseeding between`

**Randomize**`Rnd`calls.

When you call

`with the QB compatible algorithm, part of the old seed is retained. This means that if you call`

**Randomize**`several times with the same seed, you will`

**Randomize****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.

**Examples:**

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

Randomize , 1

'' Print a sequence of random numbers

For i As Integer = 1 To 10

Print Rnd

Next

Randomize , 1

'' Print a sequence of random numbers

For i As Integer = 1 To 10

Print Rnd

Next

**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
*-lang qb*dialect, a function giving the same output asin QB is used. The granularity is 24 bits.**Rnd** - 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
parameter is new to FreeBASIC.*algorithm* - QBASIC only had one algorithm (replicated in FB in algorithm number
, and set as the default in the**4***-lang qb*dialect).

**See also:**

Back to Mathematical Functions