Statsar Statistics Library

v1.0.1 for .NET

Product Guide



More Information...

Valid XHTML


Distribution Random Example (C#)

[C#]
/* COPYRIGHT(C), SIMPLEXAR SOFTWARE LIMITED, 2006-2008.
 * All rights reserved.
 *
 * Use of this copyright notice is precautionary only, and does not imply
 * publication or disclosure. The content of this work contains confidential
 * and proprietary information of Simplexar Software Limited. Any duplication,
 * modification, distribution, or disclosure in any form, in whole, or in part,
 * is strictly prohibited without express prior written permission.
 */

// Get namespaces.
using System;
using Simplexar.Statsar;
using Simplexar.Statsar.Distributions;

// Start namespace.
namespace Simplexar.Examples.Statsar
{
    /// <summary>
    /// This example shows how to generate random deviates which are
    /// distributed according to a probability distribution.
    /// </summary>
    public static class Example
    {
        /// <summary>
        /// The application entry point.
        /// </summary>
        public static void Main()
        {
            try
            {
                RunExample();
            }
            catch(Exception exception)
            {
                Console.WriteLine(exception);
            }
            Console.WriteLine("Press ENTER to terminate.");
            Console.ReadLine();
        }

        private static void RunExample()
        {
            // Create a normal distribution instance. The normal distribution
            // is a continuous probability distribution defined in terms
            // of the distribution's mean and standard deviation.
            // Many other probability distributions are available as part
            // of the Simplexar.Statsar.Distributions namespace.
            double mean = 3;
            double standardDeviation = 2.5;
            NormalDistribution distribution
                = new NormalDistribution(mean, standardDeviation);

            // Generate a random deviate which is normally distributed.
            Console.WriteLine(
                "Single deviate: {0}", distribution.GetDeviate());

            // Generate 10 random deviates which are normally distributed.
            Console.WriteLine("Multiple deviates:");
            double[] deviates = distribution.GetDeviates(10);
            foreach(double deviate in deviates)
            {
                Console.WriteLine(deviate);
            }
        }
    }
}