Is there a random sampling func开发者_如何学编程tion for the iphone? For example, if you want to flip a coin that returns heads 25% of the times it's flipped, and you want to flip it and see if you get heads this time? I googled for random sampling probability for iphone and couldn't find anything.
Since you can use any standard C function in Objective-C, you could use drand48 to obtain a random double in the range [0,1]. Success with probability p is obtained by testing if the value is < p. Ex:
if ( drand48() < 0.25 ){
// This branch will be executed 25% of the time
} else {
// This branch will be executed 75% of the time
}
精彩评论