## different seed for random number generator?

### different seed for random number generator?

hi,
can anyone tell me how I can generate a different seed if I run
srand(time(NULL)); more than one time in a second? I wrote an application
to print out a random number..if I run the application fast enough,
it will print a same number until the next second.. anyone know how I can
fix this?

Dan

### different seed for random number generator?

>    can anyone tell me how I can generate a different seed if I run
> srand(time(NULL)); more than one time in a second? I wrote an application
> to print out a random number..if I run the application fast enough,
> it will print a same number until the next second.. anyone know how I can
> fix this?

Hi Dan,

well, you could do something like this...

static long lvar=0;
srand(time(NULL)+lvar); lvar++;

This way you'll not get the same seed twice in the sec,...
BUT i'm asking myself why you call srand() more than one time.
Once the seed is set, rand() will produce random-numbers. srand() is
used to set the (pseudo-)random-number-sequence to a certain
startpoint. Why do you reset the sequence to a different (or in your
case the same) startpoint, instead of just using rand() ?

Regards Rainer

### different seed for random number generator?

>hi,
>    can anyone tell me how I can generate a different seed if I run
>srand(time(NULL)); more than one time in a second? I wrote an application
>to print out a random number..if I run the application fast enough,
>it will print a same number until the next second.. anyone know how I can
>fix this?

>Dan

you could use a seed generated from gettimeofday() ...

#include <unistd.h>
#include <sys/time.h>

long
getseed(void)
{
struct timeval tv;
if (gettimeofday(&tv, 0) == -1) {
return time(0); /* failed, fall back */
}
/* the multiply will overflow, but ...*/
return tv.tv_sec * 1000000 + tv.tv_usec;

Quote:}

--

PGP public key at http://www.panix.com/~wozzle/alane.pgp.key

### different seed for random number generator?

I just went through all this. I found this one worked well.

srand(getpid());

cause time(NULL) only sets up a new random number every second.

Good luck.

~rocker

> hi,
>         can anyone tell me how I can generate a different seed if I run
> srand(time(NULL)); more than one time in a second? I wrote an application
> to print out a random number..if I run the application fast enough,
> it will print a same number until the next second.. anyone know how I can
> fix this?

> Thanks in advance

> Dan

--
==========================================
Rocke Robertson
PWGSC-GTIS-CPS
Voice (613) 748-4604
Fax   (613) 748-4843

==========================================

### different seed for random number generator?

>         can anyone tell me how I can generate a different seed if I
> run
> srand(time(NULL)); more than one time in a second? I wrote an
> application
> to print out a random number..if I run the application fast enough,
> it will print a same number until the next second.. anyone know how I
> can
> fix this?

You should only recall srand once, at the beginning of the program.  If
you call it multiple times, and your seeding algorithm is not well
chosen, then you could potentially be getting very nonrandom numbers.

A legitimate case where the problem you're mentioning can come about
when a program (which calls srand once at the start) must be run many
times in short successioon, such that the program might be run more than
once a second (and hence, time(0) would return the same value).  The
solution is to either use a higher-resolution time function, like
gettimeofday, to build a seed which has sub-second resolution, or to
include other items in the seed, such as the process ID.

--

Alcyone Systems | web http://www.alcyone.com/max/
San Jose, CA | languages en, eo | icbm 37 20 07 N 121 53 38 W
USA | 397 days left | &tSftDotIotE
__
/  \ The more one is hated, I find, the happier one is.
\__/ Louis-Ferdinand Celine

### different seed for random number generator?

Quote:> long
> getseed(void)
> {
>         struct timeval tv;
>         if (gettimeofday(&tv, 0) == -1) {
>                 return time(0); /* failed, fall back */
>         }
>         /* the multiply will overflow, but ...*/
>         return tv.tv_sec * 1000000 + tv.tv_usec;
> }

Wouldn't tv.tv_sec * 1000000 overflow? Already the number of seconds since
the EPOCH on a 32 bit long system are just some 4 times lower than max long
on 32 bits. If you multiply it by 10^6 you will surely get outside range.
You still could get varying seeds due to the second number changing "a few"
times within a second ;)

Regards,
Tom

### different seed for random number generator?

Correction.... max unsigned long of course, when you take plain long than
max  will be just twice as big as currently returned values.

Tom

Quote:> > long
> > getseed(void)
> > {
> >         struct timeval tv;
> >         if (gettimeofday(&tv, 0) == -1) {
> >                 return time(0); /* failed, fall back */
> >         }
> >         /* the multiply will overflow, but ...*/
> >         return tv.tv_sec * 1000000 + tv.tv_usec;
> > }

> Wouldn't tv.tv_sec * 1000000 overflow? Already the number of seconds since
> the EPOCH on a 32 bit long system are just some 4 times lower than max
long
> on 32 bits. If you multiply it by 10^6 you will surely get outside range.
> You still could get varying seeds due to the second number changing "a
few"
> times within a second ;)

> Regards,
> Tom

### different seed for random number generator?

If your operating system has a /dev/urandom device you can seed the RNG
with an int from a read() on urandom each time you want to change the
seed.

--

Programmer; CE Net, Inc.
(302) 854-5440 Ext. 206 */

### different seed for random number generator?

The answer requires that you answer a question: What kind of randomness
do you need? Solutions could range from "srand(getpid()<<3^time(NULL)"
all the way to using a real entropy pool and checkpointing it to a file.

DS

> hi,
>         can anyone tell me how I can generate a different seed if I run
> srand(time(NULL)); more than one time in a second? I wrote an application
> to print out a random number..if I run the application fast enough,
> it will print a same number until the next second.. anyone know how I can
> fix this?

> Thanks in advance

> Dan

### different seed for random number generator?

> >         can anyone tell me how I can generate a different seed if I
> > run
> > srand(time(NULL)); more than one time in a second? I wrote an
> > application
> > to print out a random number..if I run the application fast enough,
> > it will print a same number until the next second.. anyone know how I
> > can
> > fix this?
[...]
> A legitimate case where the problem you're mentioning can come about
> when a program (which calls srand once at the start) must be run many
> times in short successioon, such that the program might be run more than
> once a second (and hence, time(0) would return the same value).  The
> solution is to either use a higher-resolution time function, like
> gettimeofday, to build a seed which has sub-second resolution, or to
> include other items in the seed, such as the process ID.

Also, some systems have their own RNG's; Linux, for instance, has
/dev/random and /dev/urandom, which spew random bytes (/dev/random is
truly random but requires time to "recharge" entropy; /dev/urandom is
good pseudorandom).  There is also an Entropy Generating Daemon that
serves this function on any Unix; I think a copy comes with gpg.

--

Nate Eldredge