Jump to content
  • Advertisement
Sign in to follow this  
anupgupta

Random Number Generation

This topic is 3126 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

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
Advertisement
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
A variation on mineralwasser's idea

std::vector< int > numbers;
for( int i = 0; i < 100; ++i ) {
numbers.push_back( i );
}

std::random_shuffle( numbers.begin(), numbers.end() );


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!