Best way to get a random number from 1 to 50 which ISN'T x
Posted
by Cocorico
on Stack Overflow
See other posts from Stack Overflow
or by Cocorico
Published on 2010-03-23T18:02:06Z
Indexed on
2010/03/23
18:03 UTC
Read the original article
Hit count: 258
random-numbers
|objective-c
Hi guys!
So this is probably programming 101 stuff, but I have a problem: I have 2 numbers which are between 0 and 49. Let's call them x and y. Now I want to get a couple of other numbers which are not x or y, but are also between 0 and 49 (I am using Objective C but this is more of a general theory question I think?).
Method I thought of is:
int a; int b; int c;
do { a = arc4random() % 49; } while ((a == x) || (a == y));
do { b = arc4random() % 49; } while ((b == x) || (b == y) || (b == a));
do { c = arc4random() % 49; } while ((c == x) || (c == y) || (c == a) || (c == b));
But it seem kind of bad to me, I don't know, I am just trying to learn to be a better programmer, what would be the most elegant sweet way to do this for best practices?
Thanks!
© Stack Overflow or respective owner