Please support seed parameter of System.Random(seed)
let genRandomNumbers1 count =
let rnd = System.Random(1) //the seed parameter (1) seems not be supported
List.init count (fun _ -> rnd.Next(0, 10))
let genRandomNumbers2 count =
let rnd = System.Random()
List.init count (fun _ -> rnd.Next(0, 10))
[<EntryPoint>]
let main argv =
printfn "Random Numbers1: %A" (genRandomNumbers1 20)
printfn "Random Numbers2: %A" (genRandomNumbers2 20)
0`
The first line with "Random Numbers1" should always generate the same numbers like the dotnet version, since System.Random(1) is called with a seed parameter.
This seed parameter is very useful for reproduceable Simulations.
Looks like you cannot seed the builtin RNG in JS https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript
Yes, right now Fable just calls the native Math.random which unfortunately cannot be seeded. To fix this we would have to either borrow one of the custom implementations from @kerams link or wait until the random seed proposal gets accepted (still in stage 1 though).
I implemented a PRNG library in F# that supports the System.Random interface, is seedable, and works on both Fable and .NET. It is based on the PCG algorithm and should have good statistical properties for simulations, etc. https://github.com/zanaptak/PcgRandom
I imagine it could probably be adapted and put into Fable with minimal effort if that is something you would want to consider. But I haven't done any comparison with other algorithms and there are maybe other potentially more "mainstream" options such as those from the above post. (The algorithm, among others, did receive some consideration for .NET: https://github.com/dotnet/runtime/issues/6203, https://github.com/dotnet/runtime/issues/23198, https://github.com/dotnet/runtime/issues/18996.)
Also I wonder, if taking the step to replace Math.random anyway, whether to consider implementing the actual internal .NET algorithm, as the same seed would then work both client and server side and maybe be less surprising for users. Of course it would also then have the same flaws as .NET, and users would still have to turn to external libraries for better PRNG qualities.
Thanks a lot for the info @zanaptak, those are great insights! It makes sense to implement the same algorithm as in .NET as you say, so maybe we don't need to wait until Math.random is seedable. Any thoughts? @ncave
In any case, if you need a seedable random generator that works both on .NET and Fable @rusco you can use @zanaptak library :+1: I've also added it to the community section so other users can find it easily. I hope that's ok.
Closing as we cannot currently make JS Math.random seedable.
Most helpful comment
Thanks a lot for the info @zanaptak, those are great insights! It makes sense to implement the same algorithm as in .NET as you say, so maybe we don't need to wait until
Math.randomis seedable. Any thoughts? @ncaveIn any case, if you need a seedable random generator that works both on .NET and Fable @rusco you can use @zanaptak library :+1: I've also added it to the community section so other users can find it easily. I hope that's ok.