Click here to Skip to main content
15,887,027 members
Articles / Desktop Programming / Windows Forms
Article

generate random numbers

Rate me:
Please Sign up or sign in to vote.
1.00/5 (3 votes)
30 Jun 2008CPOL 23.4K   275   8   2
Easy way to generate random real (float) or integer numbers
csRandom_source

Introduction

The main idea for this article is how to create random floating numbers in .NET. Before few days i faced problem for generating random real numbers for Stock markets and i searched for a code in CodeProject but i didn't found useful way for this problem.

RandomFloat Class

The RandomFloat class contains properties that allow user to generate random real-integer numbers between specific range and flag to turn on/off real number generator.

Using the code

Turn on or off the real number generator by set the AllowFloating property to true or false, then call the override method NextDouble()

C#
public class RandomFloat : Random
{
    protected const int DEFAULT_MIN_VAL = 1;
    protected const int DEFAULT_MAX_VAL = 10;

    protected bool m_bAllowFloat;
    protected int m_iMinVal = DEFAULT_MIN_VAL;
    protected int m_iMaxVal = DEFAULT_MAX_VAL;

    public RandomFloat()
    {
    }

    public RandomFloat(bool bAllowFloat)
    {
        m_bAllowFloat = bAllowFloat;
    }

    public bool AllowFloating
    {
        get
        {
            return m_bAllowFloat;
        }
        set
        {
            m_bAllowFloat = value;
        }
    }

    public int MinRange
    {
        get
        {
            return m_iMinVal;
        }
        set
        {
            m_iMinVal = value;
        }
    }

    public int MaxRange
    {
        get
        {
            return m_iMaxVal;
        }
        set
        {
            m_iMaxVal = value;
        }
    }

    public override double NextDouble()
    {
        int iRnd = Next(m_iMinVal, m_iMaxVal);
        double fRnd = base.NextDouble();

        if (m_bAllowFloat)
        {
            if (iRnd < 0)
                fRnd = iRnd + (fRnd * -1);
            else
                fRnd += iRnd;
        }
        else
            fRnd = iRnd;

        if (fRnd > m_iMaxVal)
            fRnd = m_iMaxVal;

        return fRnd;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
GeneralGenerate random numbers Pin
AnithaJoseph8529-Apr-09 1:38
AnithaJoseph8529-Apr-09 1:38 
GeneralComputers can't create true random numbers Pin
seesharper30-Jun-08 21:35
seesharper30-Jun-08 21:35 
Hi there!
I just wanted to point you in the right direction when it comes to generating random numbers.

You see, that you can't really use a computer to create something that is random.
It may look random at first, but numbers generated by a computer will always follow some sort of pattern.

There are some "hacks" you can do to make it look more random, but true random numbers can never come from a computer.

I suggest that you have a look at this site www.random.org for further explanation on the subject.

Regards

Seesharper

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.