generate rand number

L

Larry

Hi,

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
 
O

osmium

Larry said:
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 :-(

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",
 
O

osmium

osmium said:
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.
 
A

Andrew Poelstra

Hi,

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 :-(

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.
 
J

James Kanze

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.
 
B

Bo Schwarzstein

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.)


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.

Try boost.random or GSL routines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top