I need to create a tiny function to generate a random number ranging
from: 1000000 to 9999999. Also, the number should be unique even if I call
the function 100 times in a second...can it actually be done?
All I know for the moment is this: srand(time(0)): rand(); but it is not
that great :-(
thanks
For the range problem, look at the FAQ for the C language. You can find the
FAQ via google.
For the uniqueness problem use google to look for the magic buzzword
"shuffle",
On further thought. If you need a huge number of numbers, say over 100,000,
the shuffle solution may not be a good one. In that case, establish a
vector and put each number in it as chosen. The vector will grow
automatically to "infinite" length, as needed. Examine the vector before
issuing each number. The search will get slower and slower as time goes on
but you may be able to clever that up a bit to handle that.
Depending on how random you need this to be, you could use
something simple, like:
x = 1000000 + (x * Y mod 9000000);
Where x is a static variable that you return with each call,
and Y is anything reasonably big and relatively prime to
9000000.
That will look kinda random, and solve your uniqueness
problem.
This question might better be posed on comp.programming.
> I need to create a tiny function to generate a random number
> ranging from: 1000000 to 9999999.
Have a look at Boost's Random module:
<http://www.boost.org/doc/libs/1_41_0/libs/random/index.html>
It's included in TR1, so depending on your compiler you may be able to
use it even without Boost.
> Also, the number should be unique
> even if I call the function 100 times in a second...
Make a list of all the numbers in the range you need. Shuffle the list;
that is, sort it using a randomized comparitor function. Now you can
simply iterate through the list. This approach has a lot of up-front
overhead to create and sort the list, both memory and processor time,
but gives consistent performance for fetching the next number, no
matter how many times you call it.
Alternatively, you could create a set to hold previously-selected numbers,
and each time you need to select a new one, generate random numbers
until you find one that isn't in the set. That will avoid the up-front
cost of creating the list, but performance will suffer as more items
are added to the already-selected set.
sherm--
> I need to create a tiny function to generate a random number
> ranging from: 1000000 to 9999999. Also, the number should be
> unique even if I call the function 100 times in a second...can
> it actually be done?
So which is it? Do the numbers have to be unique, or should
they be random? (One excludes the other -- if they have to be
unique, then they can't be truely random.)
> All I know for the moment is this: srand(time(0)): rand(); but it is not
> that great :-(
The quality of the random number generated in the standard
library is a quality of implementation issue. Some are fairly
good, but a lot are horrid. It's fairly simple to implement
your own, and even simpler to use Boost's functions.
--
James Kanze
Try boost.random or GSL routines.