Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program with probability

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)

like image 980
user3437460 Avatar asked Oct 20 '22 10:10

user3437460


1 Answers

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.

like image 50
4pie0 Avatar answered Oct 23 '22 10:10

4pie0