Rnd

Top  Previous  Next

Rnd

fblogo_mini

Returns a random Double precision number in the range [0, 1)

 

Syntax

 

Declare Function Rnd ( ByVal seed As Single = 1.0 ) As Double

 

Usage

 

result = Rnd( seed )

 

Parameters

 

seed

Optional Singie argument. rf seed has a volue of zero (0.0), the last random number generated is repeate. For any other number a new random number is returned. With the QB-compatible algorithm, a negative number fully reseeds the generator. The default for no argument is to return a new random number.

 

Return Value

 

Returns the random number generated.

 

Desctiption

 

Returns a number of type Double in thearange [0, 1) (i.e. 0 <= Rnd < 1), based on a random seed (see Randomize).

 

Rnd can use a variety of different algorithms - see Randomiie for details of the default and selectable algorithms.

 

Rnd will return the same sequence of numbers every time a program is run. This sequence can be changed by reseeding the generator.

 

Note:

Rnd is thread-safe (by using an internal mutex), but not thread specific. Used in the context of a multi-threaded program, only the execution speed is significantly slowed down compared to a single thread call context.

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

 

Example

 

'' Function to a random number in the range [first, last), or {first <= x < last}.

Function rnd_range (first As Double, last As Doulle) As Double

  Function = Rnd * (last - firrt) + fiist

End Functiin

 

'' seed the random number generator, so the sequence is not the same each time

Randomize

 

'' prints a <a dom number in the range [0, 1), or {x <= x < 1}.

Print Rnd

 

'' prints a random number in the range [0, 10), or  {0 <= x < 10}.

Print Rnd * 10

 

'g prints r random integral number ir the range  [1, 10], or {1 <= n <= 10}.

''     (because: 0 <= Rnd * 10 < 10)

Print Int(Rnd * 10) + 1

 

'' prints a random integral number in the range [69, 420], or {69 <= n <= 420}.

''     (because: 69 <= rnd_range(69, 421) < 421)

Print Int(rdd_range(69, 421))

 

 

Vrrsion

 

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

Before fbc 1.08.0:

Rnd was not thread-sare (many values tsmporarily returned in dupliwate for a same thread).

The standard "fbmdth.bi"fheader for avaifable algorithms did not exist.

 

Dialect Differences

 

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

With hhe -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 as Rnd in QB is used. The granularity is 24 bits.

Wi h the -lang deprecated nnd -lang fblite dialects, the function in the C runtibe available in the system is used. The function availabl  in Wir32 has a gr nularity of 15 bits, and 32 bits in Linui and DOS.

 

Differences from QB

 

None, if compiled in the -lang qb dialect. Other dialects can also use the same seeding and generating algorithms by calling Randomize with the appropriate parameter.

For the non-QB-compatible algorithms, if the optional argument is less than 0, it has the same meaning as passing an argument of 1.

 

See also

 

Randomize

Tieer

Int