Program with probability

11,715

Is the above code an acceptable code to create a simulation with probability? Or are there any flaws in these codes which would affect the accuracy of the simulated results?

If this is "acceptable" this depends on what is your definition of acceptable. This is not correct for sure as operator % makes your probability skewed because RAND_MAX which is maximum value for rand() can be not equal to k * 100 + 99 which results in that if you imagine your 100-length parts of 0-RAND_MAX string then you can see that last part will probably not produce a full range 0-99, so you have more numbers that generates 0, 1, 2..., x but not necessary x + 1, ..., 98, 99 ( 1 more occurrence for each number in 0, 1, 2, ..., x). The inaccuracy of this approach increases with bigger divisor which doesn't divide the range evenly.

If the above codes has flaws, what would be the correct way of implementing simulation for probability?

You can use boost or if you can run C++11 then you can use standard library's uniform_int_distribution.

Share:
11,715

Related videos on Youtube

user3437460
Author by

user3437460

Updated on August 24, 2022

Comments

  • user3437460
    user3437460 over 1 year

    In the event where we need to generate probability, for example a bias coin with 75% of tossing head and 25% tossing tail. Conventionally, I will do it this way:

    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main()
    {
        int heads=0, tails=0;
        srand(time(NULL));
        number = rand() % 100 + 1;  //Generate random number 1 to 100
              if (number <= 75) //75% chance
                    heads++; //This is head
              else
                    tails++; //This is tail
    }
    

    This is a working code, however when I answered a similar question on bias coin in SO for another user, some of the users mentioned about the multiple of 100. Since the random function generates uniform distribution, I feels that the above code is good enough for simulating a probability event.

    In this past SO posts, user Bathsheba mentioned somehting about multiples of 100: Program that simulates a coin toss with a bias coin I wanted to know what are the possible problems in my code in relation to that.

    My question is: Is the above code an acceptable code to create a simulation with probability? Or are there any flaws in these codes which would affect the accuracy of the simulated results. If the above codes has flaws, what would be the correct way of implementing simulation for probability?

    Edit: With a simulated test of 10,000,000 toss. It always generates at probability of approximately 75.01%-75.07% chance for tossing head. So what problems could there be when it is generating an seemingly accurate result. (The generated results didn't seemed to be skewed)

    • Mitch Wheat
      Mitch Wheat about 10 years
      "Since the random function generates normal distribution" - no it doesn't. It generates a uniform distribution...
    • The Paramagnetic Croissant
      The Paramagnetic Croissant about 10 years
      The % operator skews the distribution.
  • SGM1
    SGM1 about 10 years
    Just adding to this, (MAX_INT % 100) isn't zero, (I'm not sure how well the distribution is for rand() but given that it is a uniform distribution for 0 to MAX_INT, all you need to do is use rand() % 4 and check for less that 4 (you want to mod by a power of 2).