Thursday, 15 April 2010

C++ Random number within range with exclusion -



C++ Random number within range with exclusion -

i want generate random number in range 0 5, example, in cases don't need number 3, need 0, 1, 2, 4, 5. how can generate random number within range, alternative exclude numbers don't need.

are numbers want exclude known @ compile time? simple lookup table should do:

static int table[] = {0, 1, 2, 4, 5}; int index = rand() % (sizeof table / sizeof *table); int number = table[index];

of course, rand() terrible pseudo random number generator, that's different topic.

c++

No comments:

Post a Comment