13

When you need to be able to generate a random number from a seed, and guarantee it be the same number across different versions of the .NET Framework and Mono Framework, as-well as across different architectures (x86, x64), what do you do?

I'm presently considering storing millions of random bytes in a file for use as a temporary solution to this problem, but I think there's probably a better way (hopefully not too much more complicated).

Mr. Smith
  • 4,288
  • 7
  • 40
  • 82
  • Why would System.Random work differently on a 64-bit computer when it uses Int32 so extensively? Have you tested the difference? –  Jun 13 '13 at 18:19
  • Perhaps implement your own random number generation algorithm (actually it had better be pseudorandom in order to be deterministic). – Rob I Jun 13 '13 at 18:19
  • ... So you want predictable random numbers? – Darren Kopp Jun 13 '13 at 18:19
  • 4
    @ebyrob "[The implementation of the random number generator in the Random class is not guaranteed to remain the same across major versions of the .NET Framework.](http://msdn.microsoft.com/en-us/library/system.random.aspx)" - MSDN – Mr. Smith Jun 13 '13 at 18:20
  • 2
    @DarrenKopp: I believe he wants *reproducible* random numbers. – Oliver Charlesworth Jun 13 '13 at 18:21
  • @Mr.Smith Yes, they reserve the right to change it between .Net 2.0 and .Net 4.5. Not between .Net 4.0 and 64-bit .Net 4.0... Working on two practically identical platforms now and working 10 years from now on either one is a very different goal. –  Jun 13 '13 at 18:22
  • My [ojrandlib](http://github.com/lcrocker/ojrandlib) is pretty portable. I've compiled in under Linux and Windows. It's pure C source. – Lee Daniel Crocker Jun 13 '13 at 18:24
  • 1
    I posted an implementation of a Mersenne Twister you can cut and paste from here: http://stackoverflow.com/a/16881065/106159 – Matthew Watson Jun 13 '13 at 18:24

3 Answers3

15

If you need a truly portable implementation, the best option would probably be to just use a custom random number generator, such as this Mersenne Twister implementation or Colin Green's Fast Random Number Generator. By controlling the implementation, you can guarantee that you'll get the same results given the same seed on any platform.

Can
  • 76
  • 10
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Colin Green's implementation of `Xorshift RNGs` seems to be exactly what I'm after, but I'm not sure what the licensing agreement is on it. `GPL` and even `LGPL` are not compatible with this project. – Mr. Smith Jun 14 '13 at 13:50
  • 1
    @Mr.Smith I'd write him directly and ask, but the linked page does include "This article is licensed under a Creative Commons Attribution 3.0 License " – Reed Copsey Jun 14 '13 at 16:07
3

There's also the Troschuetz.Random library available through NuGet which provides several "standard" randomization methods which are presumably available on other platforms as well:

Fully managed library providing various random number generators and distributions. More precisely, this library offers 7 generators (ALF, MT19937, Standard, XorShift128, NR3, NR3Q1, NR3Q2), 6 discrete distributions (Bernoulli, Binomial, Categorical, DiscreteUniform, Geometric, Poisson) and 21 continuous distributions (like Normal, Exponential, ChiSquare, Beta, and so on); moreover, it offers a random class similar to the one Python offers (TRandom).

All the hard work behind this library was done by Stefan Troschütz and optimized within this .NET package by Alessio Parma.

Jonathan B.
  • 2,742
  • 1
  • 21
  • 18
1

Got following code from Wiki: http://en.wikipedia.org/wiki/Random_number_generation

m_w = <choose-initializer>;    /* must not be zero */
m_z = <choose-initializer>;    /* must not be zero */

uint get_random()
{
    m_z = 36969 * (m_z & 65535) + (m_z >> 16);
    m_w = 18000 * (m_w & 65535) + (m_w >> 16);
    return (m_z << 16) + m_w;  /* 32-bit result */
}

This should be what you need: http://en.wikipedia.org/wiki/Linear_congruential_generator

xeranic
  • 1,391
  • 1
  • 9
  • 16
  • This algorithm is [Multiply-with-carry pseudorandom number generator](https://en.wikipedia.org/wiki/Multiply-with-carry_pseudorandom_number_generator). Had to follow [wiki edit history](https://en.wikipedia.org/w/index.php?title=Random_number_generation&oldid=562039192) to figure out – YukiNyaa May 11 '21 at 07:10