### Random Number Generation

I have a function that generates a random number

 1234567891011 int randomNumber(int l, int u) { int x; //get random number between l and u---------- unsigned seed = chrono::system_clock::now().time_since_epoch().count(); default_random_engine generator (seed); uniform_int_distribution distribution(l,u); x= distribution(generator); //------------------------------------------- return x; }

My problem is, because of the seed, calling the function twice in a row happens so fast it returns the same result. If I call the function constantly I get the same result 15 or 16 times in a row.

When I need to get different random numbers quickly are there certain ways this is usually approached?

My first thought is to get a random number from 100-999 if I need 3 random single digit numbers and then just take the 1st, 2nd, and 3rd digits. The only problem is I have to know how many times the function will be called at once and for what I'm trying to do I can't know if t will be called 3 times or 20 times in rapid succession.

Thanks.

EDIT:
I could create a vector of 20 or so ints and fill it with random 10 digit numbers generated once every second. Once the vector is full the generator stops. If I need a random number I take the first element of the vector, use the % operator to get a number in the desired range, and then erase the first element.
It's not pretty but it would work....I think. I would till love to hear some better suggestions.
Last edited on

Well you should only need to set the seed once in your program (outside of the function), then generate your random number as much as you need by calling the function.
Wouldn't I need to use a different seed every time?
You only need the seed to be different each time you run the program because if you use the same seed you will get the same sequence of numbers. If you use a different seed you get a different sequence.
Topic archived. No new replies allowed.