creating a random number between 0 to 1 continuously

20,165

Solution 1

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);

Solution 2

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.

Share:
20,165
lakshmen
Author by

lakshmen

Loves Programming and very keen in learning new things... NSMutableArray *skills = @[ @"C++", @"Python", @"Objective-C", @"Matlab", @"VBA", @"R", ]; Any questions regarding the codes, do feel free to contact me. My Email address can be obtained using this code in MATLAB: s = char(double([99 110 110 108 97 107 115 104 109 101 110 95 50 48 48 48 64 121 97 104 111 111 46 99 111 109])) clc; disp(s); disp(' '); You can connect with me in Linkedin

Updated on July 25, 2020

Comments

  • lakshmen
    lakshmen almost 4 years

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

  • Jorge Leitao
    Jorge Leitao over 10 years
    While this is true, it does not answer the question being asked. xD
  • Pete Becker
    Pete Becker over 10 years
    The random number generator is not the problem. The problem is comparing probabilities whose values are between 0 and 1 with fractions like 1/3; the values of these fractions are always 0.
  • Pete Becker
    Pete Becker over 10 years
    @J.C.Leitão - um, yes, it does answer the question. The way the code is written, it will almost always increment the stock price.
  • Jorge Leitao
    Jorge Leitao over 10 years
    @PeteBecker I think the question is related with the values of the random number itself, not the stock price.
  • Pete Becker
    Pete Becker over 10 years
    @J.C.Leitão - despite the noise from the anti-rand folks, there is no implementation on the planet that does what the question describes. The code itself is wrong, and it will steadily increase the stock price. If there are still problems after that's fixed, it might be appropriate to look at the quality of the generator. However, for a simple program like this, rand is good enough, and any problems are in the code itself.
  • tidy
    tidy about 9 years
    I think you should use rand()/((double)RAND_MAX + 1) to avoid integer overflow problem.