Random number generation
We implement random number generation by calling the mt19937 library, which implements the Mersenne Twister algorithm. This is the same library used by numpy for random number generation.
Examples
- Generate first 10 Float64 numbers from seed 2.
::
let m: Random = Random(2)
let a = empty<Float64>(10)
for (i in 0..10) {
a[i] = m.nextFloat64()
}
- Generate first 10 UInt64 integers from seed 2.
::
let m: Random = Random(2)
let a = empty<UInt64>(10)
for (i in 0..10) {
a[i] = m.nextUInt64()
}
- Generate first 10 UInt32 integers from seed 2.
::
let m: Random = Random(2)
let a = empty<UInt32>(10)
for (i in 0..10) {
a[i] = m.nextUInt32()
}
class Random
This class encapsulates the state of the mt19937 algorithm.
Methods ^^^^^^^
======================== =========== Method Description ======================== =========== Random.init([s]) Initialization. setSeed([s]): Unit Set random seed. nextUInt64(): UInt64 Generate random UInt64. nextUInt32(): UInt32 Generate random UInt32. nextFloat64(): Float64 Generate random Float64. nextFloat32(): Float32 Generate random Float32. ======================== ===========
Description ^^^^^^^^^^^
.. function:: Random.init([s])
:param s: Optional, starting seed
Initialize random number generator with the given seed. If seed is not provided,
a seed is generated using the current time.
.. function:: setSeed([s])
:param s: Optional, starting seed
Re-seed the random number generator. If seed is not provided, a seed is
generated using the current time.
.. function:: nextUInt64()
:return: UInt64
Generate next UInt64 number.
.. function:: nextUInt32()
:return: UInt32
Generate next UInt32 number.
.. function:: nextFloat64()
:return: Float64
Generate next Float64 number.
.. function:: nextFloat32()
:return: Float32
Generate next Float32 number.