# don't want to randomize two alike numbers

• 05-24-2005
randomizer
don't want to randomize two alike numbers
I have an annoying problem.

I want to randomize 7 different number between 1-35 (I know how to randomize).

The problem is: How can I randomize so I won't have two numbers alike?

I have tried vectors, lists static reference and everything. But I can't figure it out :confused:
• 05-24-2005
Darryl
OK, try using sets, keep inserting random numbers until set size is 7
Code:

```#include <set> int main() set<int> random7; while (random7.size() < 7) {     random7.insert( PutYourRandomNumberFunctionHere()) }```
• 05-24-2005
hk_mp5kpdw
Only issue with the set method is that you end up with the random values in sorted order which might be bad depending what you need to do with the numbers... but you could take care of that in other ways.
• 05-24-2005
Darryl
Quote:

Originally Posted by hk_mp5kpdw
Only issue with the set method is that you end up with the random values in sorted order which might be bad depending what you need to do with the numbers... but you could take care of that in other ways.

Ahh yea I didn't think about the sorting...the best thing to do is use vector, but just do a search of the vector first before you insert the next random number.

Code:

```#include <vector> #include <algorithm> int main() {     vector<int> random7;     while(random7.size() < 7)     {         int RandomNumber = SomeRandomNumberFunction();         if (find(random7.begin(), random7.end(), RandomNumber)==random7.end())         {             random7.push_back(RandomNumber);         }     } }```
• 05-24-2005
quzah
Here's a fun way:
Code:

```int chosen[ FOO ] = {0} int x; for ( int y = 0; y < BAR; ) {     x = rand() % FOO;     if( !chosen[ x ] )     {         chosen[ x ] = 1;         y++;     } }```
The index of the non-zero array elements is the chosen value. Add one to it if you don't like "0 through FOO -1".

Quzah.
• 05-26-2005
randomizer
Thank you guys. I love you all!!!
• 05-26-2005
JoshR
I...I....i............love you too :rolleyes:

Now that we are done with that, if you want different sets of random numbers each time, remember to set the seed using the time function in the #include <ctime.h>
• 05-26-2005
hk_mp5kpdw
Quote:

Originally Posted by JoshR
I...I....i............love you too :rolleyes:

Now that we are done with that, if you want different sets of random numbers each time, remember to set the seed using the time function in the #include <ctime.h>

:rolleyes:
• 05-26-2005
Thantos
Quote:

Originally Posted by quzah
Here's a fun way:
Code:

```int chosen[ FOO ] = {0} int x; for ( int y = 0; y < BAR; ) {     x = rand() % FOO;     if( !chosen[ x ] )     {         chosen[ x ] = 1;         y++;     } }```
The index of the non-zero array elements is the chosen value. Add one to it if you don't like "0 through FOO -1".

Quzah.

or we can use the possibility of vector<bool> being optimized:
Code:

```std::vector<bool> chosen(FOO, false); int x; for ( int y = 0; y < BAR; ) {     x = rand() % FOO;     if( !chosen[ x ] )     {         chosen[ x ] = 1;         y++;     } }```
Weee less memory :)