How can I efficiently select several unique random numbers from 1 to 50, excluding 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
21:23 UTC
Read the original article
Hit count: 312
random-numbers
|objective-c
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 way to do this for best practices?
© Stack Overflow or respective owner