开发者

Is there a random sampling function for iphone development that can simulate a coin toss?

开发者 https://www.devze.com 2023-01-01 20:49 出处:网络
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 g

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消