Public Group

storing random unique values in a vector

This topic is 2015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have done a lot of research on this problem. I am trying to store random values in a vector. I want to generate a random value from 1 to 9. Then I want to fill a vector with 5 unique random values. Then I want to output the values.
here is the code I am using.
 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v; for(int i=1;i<10;i++) v.push_back(i); random_shuffle(v.begin(), v.end()); for(vector<int>::iterator itr=v.begin(); itr != v.end(); ++itr) cout << *itr << endl; } 
basically this code takes the numbers from 1 to 9 and shuffles them and then outputs them.my question is how do I shuffle a sequence of random numbers that do not repeat. Let me know if you need more explanation.

Share on other sites
It looks like your code is doing what you expect; you feed in ten digits, shuffle them, and output the result.

Can you give some examples of input and output? What do you mean by "a sequence of random numbers that do not repeat" ? Individual digits frequently repeat themselves in random number sequences.

Share on other sites
How do I shuffle a sequence of random numbers that do not repeat

Hi Phil,

By default, the seed used by the random number generator is set to a default value each time the program starts. This causes the same sequence of random numbers. You will need to reseed the random number generator before using it.

Use the time function under the ctime header to get the current time (number of seconds since the epoch), then use that to seed the random number generator using srand:
 srand( (unsigned)time(0) );  Edited by fastcall22

Share on other sites
What you have *shouldn't* repeat, except for one thing: You never seed the random number generator.

std::random_shuffle, if I recall correctly, uses rand() under the hood (unless you pass a different generator) - though I'm not sure if that changes for C++11, nor if it's standardized that rand() is default.

Since you never call srand(), rand() is seeded with 0 - thus the random shuffle repeats each time.
Instead, seed srand() with time(NULL) at the start of your program, and hopefully the result will be more suiting to your tastes.

[Edit:] Three posts within 60 seconds of each other. =) Edited by Servant of the Lord

Share on other sites

It looks like your code is doing what you expect; you feed in ten digits, shuffle them, and output the result.

Can you give some examples of input and output? What do you mean by "a sequence of random numbers that do not repeat" ? Individual digits frequently repeat themselves in random number sequences.

well I made some changes to my code.
input should be a random number from 1 to 9
output should be a list of random numbers that do not repeat.
such as: 3 7 8 9 1 2 5 4 6
 #include <iostream> #include <vector> #include <algorithm> #include <time.h> using namespace std; int main() { srand(time(NULL)); vector<int> v; for(int i=1;i<10;i++) { int j=rand()%9+1; v.push_back(j); } random_shuffle(v.begin(), v.end()); for(vector<int>::iterator itr=v.begin(); itr != v.end(); ++itr) cout << *itr << endl; } 

Share on other sites

input should be a random number from 1 to 9
output should be a list of random numbers that do not repeat.
such as: 3 7 8 9 1 2 5 4 6

What's the connection between the input and the output? You want to only produce 9 different sequences?

Share on other sites
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

Share on other sites
Are you trying to skip numbers that have appeared already? You example isn't quite right...

Share on other sites
The output list contains a randomised ordering of the unique set of elements in the input list?

Share on other sites

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

Your code should already do that. What is the current result that you are getting that you don't want?
Why does your input have non-unique variables? Where is this input coming from - the player or is it generated by your code?

Since we are all failing to understand, could you give a bigger-picture description of why you want this, and how you intend to use it?

• 40
• 15
• 10
• 23
• 10