Click or drag to resize

Rand Function

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() AS FLOAT
Request Example View Source

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