Click here to Skip to main content
Click here to Skip to main content
Go to top

Random generators for .NET

, 2 Oct 2003
Rate this:
Please Sign up or sign in to vote.
A thin wrapper around the Boost.Random library.

Introduction

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 them 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#

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

Share

About the Author

Jonathan de Halleux
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... Pinmembersherifffruitfly6-Jan-10 14:08 
GeneralRe: Seed? Pinmemberleppie3-May-04 7:46 
GeneralRe: Seed? Pinmemberabhadresh3-May-04 8:56 
GeneralRe: Seed? Pinmembercoderforrent3-May-04 12:28 
GeneralSPAMMER Pinmemberabhadresh3-May-04 8:56 
GeneralGood stuff! PinmemberDirk Vandenheuvel9-Dec-03 4:09 
GeneralRe: Good stuff! PinmemberJonathan de Halleux9-Dec-03 6:09 
Generalnot only System.Random Pinmemberpoupou3-Oct-03 1:33 
GeneralRe: not only System.Random PinmemberJonathan de Halleux3-Oct-03 1:53 
GeneralRe: not only System.Random PinmembercusTom39-Jan-06 7:50 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 3 Oct 2003
Article Copyright 2003 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid