Battleship source for Unix w/curses

Gregory Smith greg at utcsri.UUCP
Sat Mar 22 03:02:52 AEST 1986


[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



More information about the Comp.sources.unix mailing list