www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/05/14:38:09

To: jalex AT ea DOT oac DOT uci DOT edu
Cc: djgpp AT delorie DOT com
Date: Thu, 5 Feb 1998 14:30:14 -0500
Subject: Re: Automatically Seeding Random Numbers
Message-ID: <19980205.143015.19086.1.matthew.krause@juno.com>
From: matthew DOT krause AT juno DOT com (Matthew R Krause)

On Tuesday, 03 Feb 98, Jason Alexander wrote:
 
>  Please excuse my asking what is probably a simple question, but since
>I'm teaching myself how to program in C using djgpp, I don't yet know
>how to figure out this problem.
Ahh. I'm (slowly) teaching myself C also. I need to get better at it
though, so I can take my school's Advanced Placement Test in it. If you
run across any helpful books, sites, whatever, I'd really appreciate if
you let me know about them. But anyway, on to your problem.

>  Basically, I'd like a program to (automatically) seed srand() with a
> different value each time the program is run. (Currently I require the
>user to type in a seed, which is a nuisance.)
Yes, that would be a pain.

> Initially, I thought something as simple as srand(clock()) would work,
but then I > found out that clock() only returns the number of clock
ticks since the *last* call of > clock(), so clock() always returns the
value 0 when called for the first time.
Close. What you need to do is srandom(time(0)) if you are using random,
or srand(time(0)). Don't feel _too_ dumb though, I did the same thing.

>    Is there, say, a way to get the number of milliseconds after the
last
> minute or something like that?
If I understand this correctly, biostime(0,0) from bios.h returns the
number of 18.2 Hz ticks since midnight. You could use that, although time
works just as well (I think).

>   More generally, what is the preferred way of automatically generating
>such a seed?
You got it: Use the time, or something like it. Some programs, like PGP,
ask the user to bash some keys down, and time the interval, but time is
more transpartent.
BTW: You only need to seed the function ONCE per program (or so I've been
told). Good luck and have fun.

Matt Krause
Orange, CT USA
Plan: Live to Be Immortal, or die trying.
My apologies for any annoying tag line that JUNO attaches to this. It was
added without my consent!




_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019