Click or drag to resize

Rand Function (Usual)

X#
Return a random number between 0 and 1.

Namespace:  XSharp.RT
Assembly:  XSharp.RT (in XSharp.RT.dll) Version: 2.21
Syntax
 FUNCTION Rand(
	nSeed AS USUAL
) AS FLOAT
Request Example View Source

Parameters

nSeed
Type: Usual
An optional start value.
This is the point at which the random number generator is initialized. Subsequent random numbers are then influenced by nSeed.
If you first call Rand() without nSeed, it starts as though 100001 were specified.
If you call the function with nSeed as 100001, it allows you to restart the generator.
Then, if you call the function several times without nSeed, it returns the "standard sequence" of numbers.
If nSeed is less than or equal to 0, the system time is brought into the process.

Return Value

Type: Float
Remarks
Rand() allows you to generate pseudo-random numbers. Multiple calls to Rand() always return the same random number sequence, provided that they have the same start value (nSeed) on the first call and that any subsequent calls do not specify nSeed.
Examples
This example uses Rand() to generate the same sequence whenever it runs:
X#
1// Initialize to default starting seed
2? Rand(100001.0)
3? Rand()            // Known sequence (no parameter passed)
4? Rand()            // Known sequence (no parameter passed)
This example generates the same start value as the previous example but subsequent calls accept a seed value which is also influenced by the system time:
X#
1? Rand(100001.0)
2? Rand(0.0)        // Depends on exact time of day
3? Rand(0.0)        // Time-dependent
This example generates a sequence of random numbers and then reinitializes to the same seed to reproduce the same numbers:
X#
1? Rand(100.0)    // Non-default seed value
2? Rand()            // Known sequence
3? Rand()
4? Rand(100.0)    // Regenerate same sequence again
5? Rand()
6? Rand()
This example multiplies the result by 100 to increase the range of returned values:
X#
1? Rand()*100
See Also