Quantcast
Channel: Math.NET Numerics
Viewing all articles
Browse latest Browse all 971

Updated Wiki: Random Numbers

$
0
0

Random Number Generation

The .Net Framwork comes with two (pseudo) random number sources: System.Random and System.Security.Cryptography.RNGCryptoServiceProvider. The former is commonly used throughout the .Net space for general purpose random number generation, the latter in selected cases where random data is needed for cryptoghraphic applications and more "randomness" is required, at the cost of being slower. If you're not sure which one to choose, where randomness is not critical we suggest to simply use System.Random, or MersenneTwister if it turns out to be too slow.

Math.NET Numerics provides a whole set of alternative pseudo random number generators (RNG). Yet, in order to stay fully compatible with other code expecting a traditional System.Random instance, all our providers derive from System.Random (via our AbstractRandomNumberGenerator class) and can thus be used directly in place of System.Random.

Random Number Generators

Namespace: MathNet.Numerics.Random
  • MersenneTwister: Mersenne Twister 19937 generator
  • Xorshift: Multiply-with-carry XOR-shift generator
  • Mcg31m1: Multiplicative congruental generator using a modulus of 2^31-1 and a multiplier of 1132489760
  • Mcg59: Multiplicative congruental generator using a modulus of 2^59 and a multiplier of 13^13
  • WH1982: Wichmann-Hill's 1982 combined multiplicative congruental generator
  • WH2006: Wichmann-Hill's 2006 combined multiplicative congruental generator
  • Mrg32k3a: 32-bit combined multiple recursive generator with 2 components of order 3
  • Palf: Parallel Additive Lagged Fibonacci generator
  • SystemCryptoRandomNumberGenerator: Generator using the RNGCryptoServiceProvider of the .Net framework. Note: Not available in portable builds

Seeding and thread safety

All generator constructors optionally accept two parameters: an integer for seeding and a boolean to enable thread-safety if needed. An exception is the crypto generator, where a custom seed would defeat its purpose.

If you create two instances of the same generator and seed, they will generate exactly the same number sequence. If no seed is provided, most generator fall back to a time-based seed, which is dangerous when creating multiple generators in quick succession (since time resolution is limited, some can end up with the same seed). It is therefore essential to reuse generators instead of creating new ones every time a random number is needed, or at least to have a single random source to generate random seeds for each.

For performance and design reasons, generators are not thread-safe out of the box. However, since sharing generators is recommended, thread-safe number generation can optionally be enabled with the second, boolean constructor parameter.

Non-Standard Ranges

Often you don't need a random number between 0 and 1 but in a different range, or e.g. a random integer of the full integer type range. Generating such random numbers correctly is non-trivial if the distribution should remain uniform (including at 0, min, max etc.). That's why we also provide a set of extension methods (in addition to Next and NextDouble) defined directly on System.Random, so the can be used not only on our custom RNGs but even with System.Random instances:
  • NextInt64: Returns a 64-bit signed integer greater than or equal to 0 and less than Int64.MaxValue. This is therefore the equivalent of the Next method but for 64-bit integers instead of 32-bit integers. Distributed uniformly.
  • NextFullRangeInt32: Returns a 32-bit signed integer of the full range, including 0, negative numbers, Int32.MinValue and Int32.MaxValue. Distributed uniformly.
  • NextFullRangeInt64: Returns a 64-bit signed integer of the full range, including 0, negative numbers, Int64.MinValue and Int64.MaxValue. Distributed uniformly.
  • NextDecimal: Returns a decimal floating point number greater than or equal to 0.0 and less than 1.0. Distributed uniformly.

Non-Uniform Random Numbers

If you need to generate non-uniform random numbers, you can combine any of these generators with our probability distributions, see Probability Distributions.

Random Numbers in F#

The F# extensions provide a Random module in the MathNet.Numerics.Random namespace that tries to make using our random number generators in F# a bit simpler and more idiomatic.
  • Random.seed (): generates a GUID based seed
  • Random.timeSeed (): generates a time-based seed
  • Random.system(), Random.systemWith seed
  • Random.crypto(), Random.cryptoWith threadSafe
  • Random.mersenneTwister(), Random.mersenneTwisterWith seed threadSafe
  • Random.xorshift(), Random.xorshiftWith seed threadSafe
  • Random.wh1982(), Random.wh1982With seed threadSafe
  • Random.wh2006(), Random.wh2006With seed threadSafe
  • Random.palf(), Random.palfWith seed threadSafe
  • Random.mcg59(), Random.mcg59With seed threadSafe
  • Random.mcg31m1(), Random.mcg31m1With seed threadSafe
  • Random.mrg32k3a(), Random.mrg32k3aWith seed threadSafe

Viewing all articles
Browse latest Browse all 971

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>