New Version 6.0!

Try it for free with our fully functional 60-day trial version.

Download now!

QuickStart Samples

Random Number Generators QuickStart Sample (C#)

Illustrates how to use specialized random number generator classes in the Extreme.Statistics.Random namespace in C#.

Visual Basic code F# code IronPython code Back to QuickStart Samples

using System;

using Extreme.Mathematics;
using Extreme.Mathematics.Random;

namespace Extreme.Numerics.QuickStart.CSharp
    /// <summary>
    /// Illustrates the use of the classes that implement
    /// pseudo-random number generators.
    /// </summary>
    class RandomNumberGenerators
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
            // This QuickStart Sample gives an overview of the pseudo-random
            // number generators that provide an alternative for the
            // System.Random class..

            // ExtendedRandom class

            // The ExtendedRandom class simply extends the functionality
            // of the System.Random class:
            ExtendedRandom extended = new ExtendedRandom();
            int[] intValues = new int[100];
            double[] doubleValues = new double[100];

            // The fill method fills an array of integers with random numbers
            Console.WriteLine("integer(99) = {0}", intValues[99]);

            // Or, it can generate uniform real values:
            Console.WriteLine("double(99) = {0}", doubleValues[99]);

            // All random number generators can also produce variates 
            // from any user-specified probability distribution.
            // The NonUniformRandomNumbers sample illustrates 
            // how to do this.

            // RANLUX Generators

            // The RANLUX generators are available with three different 
            // 'luxury levels.' Each level produces random numbers of 
            // increasing quality at a performance cost.
            // There are four constructors. The first constructor uses the
            // default seed and the default (lowest) luxury level:
            RanLux ranLux1 = new RanLux();

            // We can specify a seed value as well:
            RanLux ranLux2 = new RanLux(99);

            // We can specify the luxury level in the constructor:
            RanLux ranLux3 = new RanLux(RanLuxLuxuryLevel.Better);

            // Finally, we can specify both a seed and the luxury level:
            RanLux ranLux4 = new RanLux(99, RanLuxLuxuryLevel.Best);

            // All methods of System.Random and ExtendedRandom are available:
            Console.WriteLine("Integer from RanLux(Best): {0}", ranLux3.Next(100));

            // Generalized Feedback Shift Register Generator

            // This generator is implemented by the GfsrGenerator class.
            // It has three constructors. A default constructor that uses
            // a default seed value:
            GfsrGenerator gfsr1 = new GfsrGenerator();

            // A constructor that takes a single integer seed:
            GfsrGenerator gfsr2 = new GfsrGenerator(99);

            // And a constructor that takes an array of integers
            // as its seed. The maximum size of this seed array
            // is 2^14-1 = 16383.
            GfsrGenerator gfsr3 = new GfsrGenerator(new int[] {99, 17, int.MaxValue});

            // Once again, all standard methods are available.
            Console.WriteLine("Double from GFSR: {0}", gfsr2.NextDouble());

            // Mersenne Twister

            // The Mersenne Twister is a variation on the GFSR generator and,
            // not surprisingly, also has three constructors:
            MersenneTwister mersenne1 = new MersenneTwister();
            MersenneTwister mersenne2 = new MersenneTwister(99);
            MersenneTwister mersenne3 = new MersenneTwister(new int[] {99, 17, int.MaxValue});

            Console.Write("Press any key to exit.");