Despite the Random generator only being created once, the output is always the same random result (for all three test outputs).
A test snippet from a slightly larger script:
let myRandGen = System.Random()
let getRandomObject =
let options = [|"Bob"; "Jim"; "Jane"|]
let randIndex = myRandGen.Next(options.Length)
options.[randIndex]
printfn "New one: %s" getRandomObject
printfn "New two: %s" getRandomObject
printfn "New three: %s" getRandomObject
I need the output to be random for each call, which it currently isn't.
Example output:
New one: Jane
New two: Jane
New three: Jane
random seed() example to generate the same random number every time. If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function.
You can use Value Expressions to generate random values, for instance if you want to generate passwords for new users: integer values. decimals. random strings.
Software-generated random numbers only are pseudorandom. They are not truly random because the computer uses an algorithm based on a distribution, and are not secure because they rely on deterministic, predictable algorithms.
Your getRandomObject
is a value. It is evaluated once. To fix this, make getRandomObject
a function:
let getRandomObject () =
let options = [|"Bob"; "Jim"; "Jane"|]
let randIndex = myRandGen.Next(options.Length)
options.[randIndex]
and call it like so: getRandomObject ()
This works for me:
let myRandGen = System.Random()
let getRandomObject () =
let options = [|"Bob"; "Jim"; "Jane"|]
let randIndex = myRandGen.Next(options.Length)
options.[randIndex]
printfn "New one: %s" (getRandomObject())
printfn "New two: %s" (getRandomObject())
printfn "New three: %s" (getRandomObject())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With