home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Big Blue Disk 4
/
bbd04new.zip
/
BITS&PCS.TXT
next >
Wrap
Text File
|
1986-12-01
|
6KB
|
106 lines
^C^1BITS 'N PC's
^CBy Daniel Tobias
A collection of Hints, Tips and Bug Reports for all programmers and intermediate
to advanced users.
^CA BIT OF RANDOMNESS
It's time for something completely random. (As opposed to Ranndom, the title
used by Richard and Lavona Rann for their news column elsewhere in this issue.)
We will now talk about generating random numbers in your programs.
There are many reasons why you might want to have random numbers in your
program. For instance, you might be writing a card game program which must
deal hands to the players. Or, you may wish to create a simulation which
generates events at unpredictable times to test algorithms which are intended
to deal with real-world events.
For these reasons, most programming languages include functions which generate
random numbers of some sort. For instance, in BASIC, the function ^1RND^0 returns
a random real number between zero and one. To get a random integer from zero
to N-1, use ^1I = INT(RND * N)^0.
In Turbo Pascal, the equivalent construct is the function ^1random^0, which can
be used in two different ways: If it is called with no arguments, as in ^1r :=
^1random^0, it will generate a random real number between zero and one, just like
the ^1RND^0 function of BASIC. If, on the other hand, it is called with a single
integer argument, as in ^1i := random(n)^0, it will generate a random integer in the
range from zero to n-1.
These functions let you generate all the randomness you need. However, it is
not enough to use just these functions, if you want truly random numbers.
After all, a computer is a very deterministic machine, following explicit
instructions to the letter. The "random" numbers it generates are actually
the result of a mathematical formula contained in the PC's operating system.
If left to itself, it will give you the exact same random numbers every time.
That wouldn't be very good for a card game program; somebody who plays the
game frequently is likely to notice if he always gets the same hand.
But there is a solution; use the ^1RANDOMIZE^0 command in both BASIC and Pascal.
In BASIC, it can be invoked in the form ^1RANDOMIZE TIMER^0. This causes the value
of the system timer to be used to "seed" the random number generator. This
should probably be invoked after the user has been prompted to press a key to
continue, to ensure that this instruction is not always invoked the exact same
number of milliseconds after a reboot, so that it gives different values. It
is unlikely that any human will be able to time his actions to such a fine
degree as to press a key at an exact desired millisecond; so for all practical
purposes, the random number generator will be seeded with an arbitrary,
unpredictable number.
Actually, ^1RANDOMIZE^0 can be invoked with any variable or value as an
argument. This will cause the random number generator to be seeded with the
specified value. If it is a fixed value, such as 23, then you will always get
the same sequence of random numbers every time you use that value as a seed.
This can be useful when testing your program, if you'd like to try it out
repeatedly using the same "random" values.
Invoking ^1RANDOMIZE^0 with no parameters causes the seed value to be
requested from the user. This is probably not a good idea in game programs,
where a user might be able to "cheat" by picking a seed value for which he
already knows the resulting random numbers, but it might be good for use in a
program you are testing, so that you can try several runs with the same seed
value, then try out a different one.
In Turbo Pascal, the ^1randomize^0 function, with no arguments, causes the random
number generator to be arbitrarily seeded. Apparently the system clock value
is used for this seed.
I've found that when programs using Turbo Pascal's ^1randomize^0 function are
invoked more than once within a few minutes, the random number generator tends
to get seeded with a similar value, since only very low-order bits in the
system clock have changed. This causes the first couple of random numbers
generated after ^1randomize^0 to be similar. However, the random number
generator soon diverges if there is even the most minuscule difference in the
seed value; thus, to ensure maximum randomness in your program, you might
generate a few "dummy" random numbers like this:
^C^1for i := 1 to 10 do r := random
at the beginning of your program, right after ^1randomize^0 is invoked. After a
few iterations, the random numbers will become reasonably random.
^CTYPING IN LONG LISTINGS
Thanks to George Leritte for this hint: If you have to type in a long program
from a listing on paper (such as those in old-fashioned paper magazines), then
you probably know how hard it is to keep your eyes on the screen and the paper
at once; you probably keep losing your place and are likely to miss whole
lines. There's a better way, though, if you have a tape recorder. Simply
record your voice reading off the paper listing at a speed which is comfortable
for you to type. Then play back the tape, and type the program lines as you
hear them on the tape.
Better yet, how about subscribing to BIG BLUE DISK? Unlike those paper
magazines, BIG BLUE DISK programs are ready to run.
--------------------------
That's all for now; if you have any hints, tips, suggestions, or requests for
help on a PC-related topic, please send them in to us; for details, see the
article, How To Disk Us.