Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site utcsri.UUCP Path: utzoo!utcsri!greg From: greg@utcsri.UUCP (Gregory Smith) Newsgroups: net.sources,net.games Subject: Re: Battleship source for Unix w/curses Message-ID: <2376@utcsri.UUCP> Date: Fri, 21-Mar-86 12:02:52 EST Article-I.D.: utcsri.2376 Posted: Fri Mar 21 12:02:52 1986 Date-Received: Fri, 21-Mar-86 13:04:15 EST References: <314@drivax.UUCP> <11587@watnot.UUCP> <141@ucrmath.UUCP> <669@wjvax.wjvax.UUCP> Reply-To: greg@utcsri.UUCP (Gregory Smith) Distribution: net Organization: CSRI, University of Toronto Lines: 32 Summary: Use of srand() to avoid duplicate games [Hey Line Eater....... eat a little line for me.....] This is in reply to a posting that complained that the recently posted battleships game always generated the same initial patterns for ships. The rand() function, by default, always generates the same 'quasi-random' sequence. This is a feature, not a bug; it is much easier to debug things when you can reproduce the same sequence over and over again. ( I am assuming the program uses rand(); I don't know for sure ). rand() has a buddy called srand(i) which 'spins the wheel' based on the int i, so that different random sequences can be produced each time. Try: srand( (int) getpid()); This should be executed exactly once, and before any call to rand. getpid() returns a long int which is the current process id. I 'casted' the long to an int in the above fragment because the manual says that getpid returns a long and that srand needs an int. On my machine, a vax, this is redundant because they are both 32 bits, but it probably is required on many other machines. If this fix does not work, check that (1) you have srand and getpid available (2) srand wants an int. If (2) is wrong, change the cast ( i.e. the (int) ). If you do not have getpid, use something that returns a number based on the time, or something like that. If you don't have srand(), you're on your own :-) Hope this helps. -- "No eternal reward will forgive us now for wasting the dawn" -J. Morrison ---------------------------------------------------------------------- Greg Smith University of Toronto ..!decvax!utzoo!utcsri!greg