# don't want to randomize two alike numbers

This is a discussion on don't want to randomize two alike numbers within the C++ Programming forums, part of the General Programming Boards category; I have an annoying problem. I want to randomize 7 different number between 1-35 (I know how to randomize). The ...

1. ## 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

2. 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())
}```

3. 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.

4. 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);
}
}
}```

5. 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.

6. Thank you guys. I love you all!!!

7. I...I....i............love you too

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>

8. Originally Posted by JoshR
I...I....i............love you too

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>

9. 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