Velocity Reviews > C++ > generate rand number

# generate rand number

Larry
Guest
Posts: n/a

 01-26-2010
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

osmium
Guest
Posts: n/a

 01-26-2010
"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
Guest
Posts: n/a

 01-26-2010
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.

Andrew Poelstra
Guest
Posts: n/a

 01-26-2010
On 2010-01-26, Larry <(E-Mail Removed)> 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.

James Kanze
Guest
Posts: n/a

 01-27-2010
On Jan 26, 1:54 pm, "Larry" <(E-Mail Removed)> 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

Bo Schwarzstein
Guest
Posts: n/a

 01-28-2010
On Jan 27, 9:25*am, James Kanze <(E-Mail Removed)> wrote:
> On Jan 26, 1:54 pm, "Larry" <(E-Mail Removed)> 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.