Sign in to follow this  
anupgupta

Random Number Generation

Recommended Posts

Hello I am currently working on a 2D project which requires me to generate a random number and set the positions of my user player and AI players accordingly... I am using rand() function of C/C++ to generate the random numbers...and my MAX SIZE is 100...so while using the above rand function sometimes gives me the same generated values...want i am looking is to generate different random numbers at different times between 0 - 100...without getting same generated values i have some code from the internet..but i am not liking the logic in it... please can someone give some advice on it... Any suggestions would be of great help... Thank you

Share this post


Link to post
Share on other sites
How about something like this?


std::vector<int> numbers;
std::vector<int>::iterator iter;

for (int i=0; i<=100; ++i)
numbers.push_back(i);

// print numbers from 0 to 100 in random order
while(!numbers.empty())
{
int index = rand() % numbers.size();
iter = numbers.begin() + index;
printf("Random number: %i", *iter);
numbers.erase(iter);
}




(Not tested, though.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this