Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 437
lakshmen Avatar asked Sep 30 '13 10:09

lakshmen


2 Answers

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);
like image 116
4pie0 Avatar answered Sep 29 '22 18:09

4pie0


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.

like image 24
Pete Becker Avatar answered Sep 29 '22 20:09

Pete Becker