65.9K
CodeProject is changing. Read more.
Home

Random Generators for .NET

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.33/5 (9 votes)

Oct 3, 2003

2 min read

viewsIcon

79806

downloadIcon

252

A thin wrapper around the Boost.Random library

Why?

There are a large number of random generators available in C++, through the Boost.Random library.

Each generator has different properties, speed and domain, that makes it more suitable in specific applications.

Unfortunately, the .NET Framework provides the System.Random class only. So here goes this article.

Quick Example

The generators are used in a similar way to the System.Random object:

  1. Create a new generator, you must provide the seed:
    using Boost.Random;
    uint seed; // you must generate that
    
    // Mt11213b is the famous mersenne twister
    Mt11213b gen = new Mt11213b( seed );
  2. Call Next or NextDouble:
    rnd.Next(); // returns int
    rnd.NextDouble(); // returns double

Generators

All the generators have kept their Boost name, but in camel notation. If you want more details about their properties, take a look at the Boost.Random page.

Boost class .NET wrapper class
minstd_rand MinStdRand
minstd0_rand MinStd0Rand
ecuyer1988 Ecuyer1988
kreutzer1986 Kreutzer
HELLEKALEK1995 Helleklek1995
mt11213b Mt11213b
mt19937 Mt19937
lagged_fibonacci607 LaggedFibonacci607
lagged_fibonacci1279 LaggedFibonacci1279
lagged_fibonacci2281 LaggedFibonacci2281
lagged_fibonacci3217 LaggedFibonacci3217
lagged_fibonacci4423 LaggedFibonacci4423
lagged_fibonacci9689 LaggedFibonacci9689
lagged_fibonacci19937 LaggedFibonacci19937
lagged_fibonacci23209 LaggedFibonacci23209
lagged_fibonacci44497 LaggedFibonacci44497

Tests

Here are the result tests on 1000000 random generation. (you can do that test using the demo program).

Generator Timing in seconds
Random.Next 0,0857201124723952
Ecuyer1988.Next 0,574264885620938
Hellekalek1995.Next 0,958088807376356
MinStd0Rand.Next 0,15938422341387
MinStdRand.Next 0,152292260608541
Mt11213b.Next 0,0389937827293692
Mt19937.Next 0,0402464559043119
Random.NextDouble 0,0438223293742641
Ecuyer1988.NextDouble 0,618387532493655
Jellekalek1995.NextDouble 1,00235448918787
MinStd0Rand.NextDouble 0,214150401796876
MinStdRand.NextDouble 0,2078839629059
Mt11213b.NextDouble 0,10741672475133
Mt19937.NextDouble 0,108539772512987
LaggedFibonacci607.NextDouble 0,0276551908133576
LaggedFibonacci1279.NextDouble 0,0272001050412832
LaggedFibonacci3217.NextDouble 0,0283938321769946
LaggedFibonacci4423.NextDouble 0,0293869751602508
LaggedFibonacci44297.NextDouble 0,0299146958621836

References

  1. Boost.Random library
  2. High-Performance Timer in C#

History

  • 3rd October, 2003: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.