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)