Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!