🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.games
Article #2458

Init randomizer (was: Battleship source for Unix w/curses)

#2458
From: KW Heuer
Date: Wed, 26 Mar 1986 15:10
31 lines
1885 bytes
In article <4336@dartvax.UUCP> dartvax!davidk (David C. Kovar) writes:
>>That's easy. Just insert a "srand(getpid());" somewhere in the beginning
>>of the program, before the first "rand" call.
>
>That'll still have the same problem. getpid() will return the same pid
>each time the same user runs it, thus you would get the same seed for
>rand each time. Try srand(time()); instead.

[0]  You seem to be confusing getpid() with getuid().  On UNIX[tm], you
get a new process each time you run the program, so getpid() will return
a unique identifier.  On some non-UNIX systems (with no fork/exec) you
may get the same ID each time.

[1]  time() expects an argument.  You need "time((long *)0)".

[2]  time() returns a long.  It must be declared as such.

[3]  srand() expects an int.  The result of time() must be cast.
(IMPORTANT: effects [2] and [3] do NOT cancel out if you ignore both!)

[4]  I once ran the same program twice within one second (the resolution
of the UNIX clock) and got the same seed.  Since then I've always used
both time() and getpid(), which seems to be a reasonable idea.  (Messy?
Well, it's only done once per program!)  (I wish srand(0) would do this
automatically!

Summary:

extern	long	time();
...
srand((int)time((long *)0) + getpid());
Technical Details
Message-ID: <663@bentley.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!cbosgd!ihnp4!bentley!kwh
References: <4336@dartvax.UUCP>

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 ggr 10/10/85; site bentley.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!cbosgd!ihnp4!bentley!kwh
From: kwh@bentley.UUCP (KW Heuer)
Newsgroups: net.games,net.lang.c
Subject: Init randomizer (was: Battleship source for Unix w/curses)
Message-ID: <663@bentley.UUCP>
Date: Wed, 26-Mar-86 10:10:56 EST
Article-I.D.: bentley.663
Posted: Wed Mar 26 10:10:56 1986
Date-Received: Fri, 28-Mar-86 07:14:02 EST
References: <4336@dartvax.UUCP>
Organization: AT&T Bell Laboratories, Liberty Corner
Lines: 31