Click here to Skip to main content
15,881,757 members
Articles / Web Development / ASP.NET

Random Generators for .NET

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
2 Oct 20032 min read 78.9K   252   32   10
A thin wrapper around the Boost.Random library
This article presents a set of random number generators, based on 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:
    C#
    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:
    C#
    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.


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions

 
GeneralMore interesting that speed tests... Pin
sherifffruitfly6-Jan-10 14:08
sherifffruitfly6-Jan-10 14:08 
GeneralRe: Seed? Pin
leppie3-May-04 7:46
leppie3-May-04 7:46 
GeneralRe: Seed? Pin
abhadresh3-May-04 8:56
abhadresh3-May-04 8:56 
GeneralRe: Seed? Pin
coder4rent3-May-04 12:28
coder4rent3-May-04 12:28 
GeneralSPAMMER Pin
abhadresh3-May-04 8:56
abhadresh3-May-04 8:56 
GeneralGood stuff! Pin
Dirk Vandenheuvel9-Dec-03 4:09
Dirk Vandenheuvel9-Dec-03 4:09 
GeneralRe: Good stuff! Pin
Jonathan de Halleux9-Dec-03 6:09
Jonathan de Halleux9-Dec-03 6:09 
Generalnot only System.Random Pin
poupou3-Oct-03 1:33
poupou3-Oct-03 1:33 
GeneralRe: not only System.Random Pin
Jonathan de Halleux3-Oct-03 1:53
Jonathan de Halleux3-Oct-03 1:53 
GeneralRe: not only System.Random Pin
cusTom39-Jan-06 7:50
cusTom39-Jan-06 7:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.