Click here to Skip to main content
14,927,870 members
Articles / Web Development / ASP.NET
Posted 2 Oct 2003


32 bookmarked

Random Generators for .NET

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
2 Oct 20032 min read
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.


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


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


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


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


  • 3rd October, 2003: Initial version


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.


About the Author

Jonathan de Halleux
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 (

Comments and Discussions

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