# generate rand number

Discussion in 'C++' started by Larry, Jan 26, 2010.

1. ### LarryGuest

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

Larry, Jan 26, 2010

2. ### osmiumGuest

"Larry" wrote:

> 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

For the uniqueness problem use google to look for the magic buzzword
"shuffle",

osmium, Jan 26, 2010

3. ### osmiumGuest

osmium wrote:

> "Larry" wrote:
>
>> 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",

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.

osmium, Jan 26, 2010
4. ### Andrew PoelstraGuest

On 2010-01-26, Larry <> wrote:
> 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.

Andrew Poelstra, Jan 26, 2010
5. ### James KanzeGuest

On Jan 26, 1:54 pm, "Larry" <> wrote:

> 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

James Kanze, Jan 27, 2010
6. ### Bo SchwarzsteinGuest

On Jan 27, 9:25 am, James Kanze <> wrote:
> On Jan 26, 1:54 pm, "Larry" <> wrote:
>
> > 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.

Bo Schwarzstein, Jan 28, 2010