storing random unique values in a vector

Started by
15 comments, last by Servant of the Lord 11 years, 5 months ago
input should be a list of numbers from 1 to 9 generated randomly that can repeat
output should be a list of numbers from 1 to 9 that do not repeat
input: 1,3,4,2,1,5,5,9,8
output: 3,1,4,2,5,9,8

If this is the case Phil, then you need to be generating a random set of integers from 1 to 9, not a sequence of integers from 1 to 9:

// v is always: 1 2 3 4 5 6 7 8 9 (no duplicates, ever)
for(int i=1;i<10;i++)
v.push_back(i);
random_shuffle(v.begin(),v.end()); // (elements might be in a random order, but there still aren't any duplicates)

// v could be: 9 1 8 6 7 4 6 3 2
for(int i=1;i<10;i++)
v.push_back( rand()%9 + 1 ); // (may have duplicates)

(See the reference on rand. rand() returns an integer between 0 and RAND_MAX -- usually 32767 -- and the modulus wraps that around the range between 0 and 8, and adding one brings it between 1 and 9.)

Then, you'll need to include algorithm, such that you can use sort to move duplicates together, then use unique to move the duplicate values to the back. Once the unique values are in front, you can remove the duplicates from v by using the iterator returned by unique:

sort( v.begin(), v.end() );
v.erase( unique(v.begin(),v.end()), v.end() );


Edit:
Hurr durr, I read can documentation good.
Advertisement
@fastcall: Ah, good comprehension. I was misunderstanding what he meant.
Unless I'm mistaken, std::unique only removes consecutive duplicates. That means a sequence like "1 2 1" is still completely possible. Or am I misunderstanding some constraint as well?

Unless I'm mistaken, std::unique only removes consecutive duplicates. That means a sequence like "1 2 1" is still completely possible. Or am I misunderstanding some constraint as well?


I was thinking that too, but deferred since I'm too lazy to look it up.

...

Nope. Still too lazy. Sry
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
The easiest way to do it is to insert the numbers in a std::set (so there will be no duplicates), then copy to a std::vector and finally shuffle then.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

Yes, std::unique only handles consecutive duplicates. The input list could be sorted beforehand, or alternatively it could be changed to a std::set, preventing duplicates occurring in the first place.

Unless I'm mistaken, std::unique only removes consecutive duplicates. That means a sequence like "1 2 1" is still completely possible.

Which is what I think the OP was wanting. His current method (generate array from 0 to 9, shuffle array) provides unique elements already.

If the OP *wants* repeats, but just not two in a row of the same number, std::unique would help with that.
Otherwise, I have no clue what the OP wants, since his original code already gives him only uniques.

This topic is closed to new replies.

Advertisement