41 Random
HardThe random packages generates pseudo random numbers. The random class sets a seed in which it uses to generate random numbers, instance with the same seed will generate the same numbers. By default the seed is randomly generated, therefore if you do not initialise the seed yourself the code execution will be different upon every execution.
Here, the seed is manually set to 3 and thus repeated execution of the code results in the same “random” numbers
random.cj
import std.random.*
main() {
let m: Random = Random()
// Creates a Random object and sets a seed for obtaining the random object.
m.seed = 3
// The object can also be of the Bool type.
let b: Bool = m.nextBool()
let c: Int8 = m.nextInt8()
let d: Float16 = m.nextFloat16()
print("b=${b},")
println("c=${c} d=${d}")
return 0
}
// Output:
// b=true
// c=-25 d=0.728027