0

Reading the documentation for C#'s built-in Random class, it describes how the same seed can't be relied upon to produce the same set of numbers from version to version: https://learn.microsoft.com/en-us/dotnet/api/system.random?view=netcore-3.1#notes-to-callers

I want to be able to generate random numbers using a seed and have that seed always continue to work regardless of .NET version or device. Not being able to upgrade from one version of .NET to another because of seed incompatibility would not be an option. What is the standard practice to do protect against this? My current best idea is to have an RNG algorithm built into the project, but I feel like there must be better options?

BurkusCat
  • 41
  • 7
  • If you want to have complete control over this, then it's probably best to use your own RNG class - unless you want it it be cryptographically secure, and then it's much more tricky to ensure that your implementation is safe. But something like an XOR-Shift RNG should be fine otherwise. I can post a c# implementation of [this algorithm](http://scv.bu.edu/examples/random_numbers/xoroshiro128_plus/C/xorshift1024star.c) if you need one. – Matthew Watson Sep 20 '20 at 13:30
  • It does not need to be cryptographically secure. Thanks for the link! I will take a look at implementing that in C# or another published C# RNG algorithm! – BurkusCat Sep 20 '20 at 13:35
  • I've got a port of that I wrote that I can post if you like. It implements System.Random so it's a drop-in replacement (other than the constructor which needs 64 bits of seed) – Matthew Watson Sep 20 '20 at 13:35
  • Does this answer your question? [How to sync a PRNG between C#/Unity and Python?](https://stackoverflow.com/questions/59829276/how-to-sync-a-prng-between-c-unity-and-python) – Peter O. Sep 20 '20 at 14:14
  • You need to use your own implementation. Feel free to look at [mine](https://github.com/koszeggy/KGySoft.CoreLibraries/blob/master/KGySoft.CoreLibraries/CoreLibraries/FastRandom.cs), which is based on the [XorShift+](https://en.wikipedia.org/wiki/Xorshift#xorshift+) algorithm. – György Kőszeg Sep 20 '20 at 14:46

0 Answers0