objective c - how do I "roll" for a 25% chance to get YES or NO? -
i'd randomly generate yes or no based on percentage
so want "roll" , have 25% chance hit yes, 75% chance hit no
was hoping point me in right direction on best way this, maybe article or something?
all of other answers seem focus on percentage of yes being 25%, probability of 1/4. if percentage arbitrary integer between 0 , 100?
bool randomboolwithyespercentage(int percentage) { return arc4random_uniform(100) < percentage; } call percentage, randomboolwithyespercentage(25).
and if percentage can fractional, 37.6%? need more sophisticated. should suffice:
bool randomboolwithyespercentage(double percentage) { double r = 100 * (double)arc4random() / ((double)uint32_max + 1); return r < percentage; }
Comments
Post a Comment