You are here:

C++/random numbers in c++ without repeating the produce numbers

Advertisement


Question
how to make random numbers in c++ without repeating the
generated numbers? plsss help...ty

Answer
Hello edsel, thank you for the question.

If you want to create a random number in C++, there is a function called rand() that returns a random integer.

In order to seed the random number generator, you must call srand() prior to calling rand:

srand(unsigned int(time(0)));
int ranNum = rand();

rand() will return any random number, paying no attention to whether or not they repeat. If you want to display non-repeating random numbers, I'd suggest storing the return values of rand() in a vector, and then comparing the return of rand() against the vector values, simply calling it again if it repeats.

I hope this helps.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.