[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: TML Pascal II Random Number
limtc wrote:
I have an answer for myself, but not sure whether this is the best
solution for TML Pascal. Any suggestion appreciated.
I found that the Random function is actually provided by QuickDraw,
and it also has a SetRandSeed function. So I use the GetTick function
in the MiscTool and do this:
SetRandSeed(GetTick)
x := abs(Random);
This will give me a number between 0 to 32768, but then I have to
divide down to the range that I really wanted. Looks like a hardway to
do something simple, but is there a better method?
The ways of generating pseudorandom numbers is legion--and many
of them are of low quality and/or low performance.
If you are re-seeding from a timer value at each call, then you are
1) getting a *really* random first value, and 2) getting non-random
values for subsequent calls unless some indetermnate time event occurs
between calls, like a user interaction.
If you divide the number to get a value in a particular range, use
the remainder, not the quotient, since the remainder changes most
rapidly.
If you prefer pseudorandom numbers, so that you can re-run a program
and reproduce its behavor deterministically, you should Google for
"pseudorandom number generator" and program one that fits your needs.
A good generator will only be one or two lines of code.
You must remember that pseudorandom numbers are not actually random
numbers. As John von Neumann famously said, "Anyone who uses arithmetic
methods to produce random numbers is in a state of sin." ;-)
-michael
NadaNet file server for Apple II computers!
Home page: http://members.aol.com/MJMahon/
"The wastebasket is our most important design
tool--and it's seriously underused."