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

  • 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

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.

Viewing all articles
Browse latest Browse all 971

Trending Articles



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