42

I'm looking for a performant, reasonably robust RNG using no special hardware. It can use mathematical methods (Mersenne Twister, etc), it can "collect entropy" from the machine, whatever. On Linux/etc we have a drand48() which generates 48 random bits. I'd like a similar function/class for C++ or C# which can generate more than 32 bits of randomness and which low-order bits are equally as random as high-order bits.

It doesn't have to be cryptographically secure but it must not use or be based on the C-language rand() or .NET System.Random.

Any source code, links to sources, etc. would be appreciated! Failing that, what TYPE of RNG should I be looking for?

4
  • Why not System.Random? I know that rand is poor, is System.Random poor as well? If so, that's sad :( Sep 11, 2012 at 0:36
  • 3
    The justification for closing this question does not seem correct at all.
    – bames53
    Sep 11, 2012 at 3:21
  • @bames53 I've posted a reopen vote now. You seem to have enough rep too, so do it. :-D Oct 10, 2014 at 15:32
  • I suppose that now you can<br/>simply google
    – Sergio
    Feb 1, 2017 at 6:08

5 Answers 5

31

For C++, Boost.Random is probably what you're looking for. It has support for MT (among many other algorithms), and can collect entropy via the nondet_random class. Check it out! :-)

0
8

The Gnu Scientific Library (GSL) has a pretty extensive set of RN generators, test harness, etc. If you're on linux, it's probably already available on your system.

7

Watch out for the Gnu Scientific Library. It's licensed under the GPL rather than LGPL.

As other folks mentioned, the Boost random classes are a good start. Their implementation conforms to the PRNG code slated for TR1:

http://www.boost.org/doc/libs/1_35_0/libs/random/index.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1452.html

If you have a recent version of the G++ compiler, you may find the TR1 libraries already included

5

C++11 has adopted a robust random number library based on boost.random. You can access a number of random number engines using different algorithms to meet your quality, speed, or size requirements. Quality implementations will even provide access to whatever non-deterministic RNG your platform offers via std::random_device.

In addition there are many adaptors to produce specific distributions, eliminating the need to do such manipulation by hand (something often done incorrectly).

#include <random>

2

Boost.Random is my first choice for RNG

http://www.boost.org/doc/libs/random

1
  • You could find Boost random by just googling thats why i dint provide any links. Now i have updated it with the link to lib. Sep 11, 2012 at 6:56