Is there a method in objective-c that allows you to pseudorandomly decide between two ints? Or is there a quick way to implement this?
Asked
Active
Viewed 290 times
0
-
1For a detailed discussion on Random numbers in objective-c (including why you shouldn't use rand!) see http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c. – RB. Apr 21 '11 at 08:55
3 Answers
4
Use the following:
int number = arc4random() % 2;
if(number==0){
//pick one number
}else{
//pick other number
}

Tovi7
- 2,793
- 3
- 23
- 27
-
-
That is a uselessly heavy calculation. Why arc4? No simple random that returns float in between 0 and 0,(9) in ObjC? – Apr 25 '11 at 11:38
0
in C++ that would be result = rand() < 0.5 ? int1 : int2
why can't you build the same command in ObjC?
-
1
-
Why? `rand()` returns float. May be it's Linux-specific? Anyway, I assume it is easy enough to figure out what I meant with this code. – Apr 22 '11 at 05:26
-