Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

.NET random number generators and distributions

Rate me:
Please Sign up or sign in to vote.
4.89/5 (118 votes)
29 May 200720 min read 416.1K   29.1K   235  
Presents a fully managed class library providing various random number generators and distributions
/*
 * Copyright � 2006 Stefan Trosch�tz (stefan@troschuetz.de)
 * 
 * This file is part of Troschuetz.Random Class Library.
 * 
 * Troschuetz.Random is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Distribution.cs, 27.03.2007
 * 
 * 09.08.2006: Initial version
 * 21.09.2006: Changed the access modifier of field "generator" from protected to private 
 *               and made it accessible through the new protected property "Generator"
 * 24.09.2006: All exceptions are instantiated with localized messages
 * 27.03.2007: Declared the Reset method as virtual, so it can be overridden in derived classes
 * 
 */

using System;
using Troschuetz.Random.Resources;

namespace Troschuetz.Random
{
	/// <summary>
    /// Declares common functionality for all random number distributions.
	/// </summary>
	public abstract class Distribution
    {
        #region instance fields
        /// <summary>
        /// Gets or sets a <see cref="Generator"/> object that can be used as underlying random number generator.
        /// </summary>
        protected Generator Generator
        {
            get
            {
                return this.generator;
            }
            set
            {
                this.generator = value;
            }
        }

        /// <summary>
        /// Stores a <see cref="Generator"/> object that can be used as underlying random number generator.
        /// </summary>
        private Generator generator;

        /// <summary>
        /// Gets a value indicating whether the random number distribution can be reset, so that it produces the same 
        ///   random number sequence again.
        /// </summary>
        public bool CanReset
        {
            get
            {
                return this.generator.CanReset;
            }
        }
        #endregion

        #region construction
        /// <summary>
        /// Initializes a new instance of the <see cref="Distribution"/> class, using a 
        ///   <see cref="StandardGenerator"/> as underlying random number generator.
        /// </summary>
        protected Distribution()
            : this(new StandardGenerator())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Distribution"/> class, using the specified 
        ///   <see cref="Generator"/> as underlying random number generator.
        /// </summary>
        /// <param name="generator">A <see cref="Generator"/> object.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="generator"/> is NULL (<see langword="Nothing"/> in Visual Basic).
        /// </exception>
        protected Distribution(Generator generator)
        {
            if (generator == null)
            {
                string message = string.Format(null, ExceptionMessages.ArgumentNull, "generator");
                throw new ArgumentNullException("generator", message);
            }
            this.generator = generator;
        }
        #endregion

        #region virtual instance methods
        /// <summary>
        /// Resets the random number distribution, so that it produces the same random number sequence again.
        /// </summary>
        /// <returns>
        /// <see langword="true"/>, if the random number distribution was reset; otherwise, <see langword="false"/>.
        /// </returns>
        public virtual bool Reset()
        {
            return this.generator.Reset();
        }
        #endregion

        #region abstract members
        /// <summary>
		/// Gets the minimum possible value of distributed random numbers.
        /// </summary>
        public abstract double Minimum
		{
			get;
		}

        /// <summary>
		/// Gets the maximum possible value of distributed random numbers.
        /// </summary>
        public abstract double Maximum
		{
			get;
		}

        /// <summary>
		/// Gets the mean of distributed random numbers.
        /// </summary>
        public abstract double Mean
		{
			get;
		}
		
		/// <summary>
		/// Gets the median of distributed random numbers.
		/// </summary>
        public abstract double Median
		{
			get;
		}

        /// <summary>
		/// Gets the variance of distributed random numbers.
        /// </summary>
        public abstract double Variance
		{
			get;
		}
		
		/// <summary>
		/// Gets the mode of distributed random numbers.
		/// </summary>
        public abstract double[] Mode
		{
			get;
		}
		
		/// <summary>
		/// Returns a distributed floating point random number.
		/// </summary>
		/// <returns>A distributed double-precision floating point number.</returns>
        public abstract double NextDouble();
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions