views:

104

answers:

1

Is there a random sampling function 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.

+1  A: 

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
}
Michael Aaron Safyan
I ended up using arc4random() instead, but that's basically what I did. Thank you for your help.
William