# random numbers

• 10-24-2008
mesmer
random numbers
I know that you can scale random numbers down to a range that you want. For instance I know that I can generate a random number between [m,n] inclusive.
What I want to do is generate a random number between [m,n], BUT I want to exclude certain numbers in this range. For example let's say that I wanted to generate a random number between 4 and 10,but I want to exclude 6 and 8. How could I do that? Is it just as simple as something like this:

rand() !=6 && !=8;

I know that the above code is not correct,but am I on the right track?

As an addendum as I continue to generate random numbers I want to exclude the random numbers that I have generated. For example lets say that I generate 7, now I want to exclude 6,8,and 7
• 10-24-2008
QuantumPete
No, you can't do it like that. You will need to store every random number you generated in an array or linked list and then discard a generated random number if it is in that list.

QuantumPete
• 10-24-2008
C_ntua
Code:

```int excluded[] = {6,8}; int genRand() {     int myNumber = (rand() % (n+1)) + m;     for (int i = 0; i < sizeof(excluded) / sizeof(excluded[0]); ++i)         if (myNumber == excluded[i]) {             myNumber = genRand();             break;         }     return myNumber; }```
or something like that
• 10-24-2008
tabstop
Perhaps you want a random shuffle of the allowed numbers.
• 10-24-2008
mesmer
Quote:

Originally Posted by C_ntua
Code:

```int excluded[] = {6,8}; int genRand() {     int myNumber = (rand() % (n+1)) + m;     for (int i = 0; i < sizeof(excluded) / sizeof(excluded[0]); ++i)         if (myNumber == excluded[i]) {             myNumber = genRand();             break;         }     return myNumber; }```
or something like that

Thanks for the help, have not had time to try this out yet. Can't wait to get to my editor.