lakshmen
lakshmen

Reputation: 29064

creating a random number between 0 to 1 continuously

I am trying to model a stock price movement in C++. I need to create a random number between 0 to 1.

But it seems that the random number generator value keeps increasing and is not really random.

The code looks like this:

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<time.h>

using namespace std;

int main()
{
    double stockPrice = 25;
    int start = 0, end = 0;
    start = clock();

    srand (time(NULL));
    cout << (double) rand() / (double) (RAND_MAX) << endl;
    system("pause");

    while(stockPrice > 18)
    {
        if(stockPrice == 20)
        {
            double probability = (rand()/(double)RAND_MAX);
            if(probability <= (1/10))
            {
                stockPrice = stockPrice-1;
            }
            else
            {
                stockPrice = stockPrice +1;
            }
        }
        else if (stockPrice < 20)
        {
            double probability = (rand()/(double)RAND_MAX);
            if(probability <= (1/3))
            {
                stockPrice = stockPrice -1;
            }
            else
            {
                stockPrice = stockPrice +1;
            }
        }
        else 
        {
            double probability = (rand()/(double)RAND_MAX);
            if(probability <= (2/3))
            {
                stockPrice = stockPrice -1;
            }
            else
            {
                stockPrice = stockPrice +1;
            }
        }
        cout << stockPrice << endl;
    }

    end = clock();

    double t = (double)(start-end)/CLOCKS_PER_SEC;
    cout << t << endl;
    system("pause");
}

Not sure how to solve this.. Need some guidance...

Upvotes: 5

Views: 20291

Answers (3)

4pie0
4pie0

Reputation: 29724

Need some guidance...

guidance 1:

correct comparisons, you should use

double probability = (rand()/(double)(RAND_MAX + 1));
                                               ^
                                        for better scaling

because currently in line if(probability <= (1/10)) you are comparing with 0 because of conversion 1/10 1/3 and 2/3 to integer

guidance 2:

after all you might use generator with better statistical properties

#include <random>
#include <iostream>

    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);
    double uniformOn01 = dis(gen);

Upvotes: 8

Pete Becker
Pete Becker

Reputation: 76245

if(probability <= (1/3))
    {
    stockPrice = stockPrice -1;
    }
else
    {
    stockPrice = stockPrice +1;
    }

Since probabililty is never negative, this code will almost always increment the value of stockPrice. The only time it won't is when probability is 0. That's because 1/3 is integer division, and its value is 0. Change all of these fraction to something like 1.0/3 and things will be much better. And this has nothing to do with the quality of the random number generator. Some folks get so exercised when they see rand that they don't see anything else.

However, there is a flaw in the scaling in the code. Instead of

double probability = (rand()/(double)RAND_MAX);

use

double probability = (rand()/(double)(RAND_MAX + 1));

As originally written, the value of probability will be 1 whenever rand() produces the value RAND_MAX, and it will produce other values much more often.

Upvotes: 1

Michael
Michael

Reputation: 2250

In all of your cases where you compare the probability you have integer division, which all result in 0... instead of (2/3) you wanted (2/(double)3) or 2/3.0.

Upvotes: 0

Related Questions