Vote count:
1
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 normal distribution, I feels that the above code is good enough for simulating a probability event.
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?
asked 1 min ago
Aucun commentaire:
Enregistrer un commentaire