Board index » delphi » random number generator for simulation purposes

random number generator for simulation purposes

Good afternoon everybody,

I'm looking for pointers to random number generators (which are
"relatively easy" programmable in TurboPascal). And I mean GOOD random number,

not the procedure 'random' or 'rand' in TurboPascal, because the generator in
TurboPascal is flawed. If anyone could give me any hints, pointers, book
titles, references to articles or whatever I'd be most delighted. I've been
searching the WWW and God knows what else, but there doesn't seem to be
anything I can use.

Any help would be extremely appreciated (I'm aware of the fact that this
message could have been posted as well have to comp.simulation, but that
newsgroup contained 0.0 messages, from which I inferred that no one reads that

newsgroup).

Regards,

Casper van Eersel
E.C.vEer...@kub.nl

 

Re:random number generator for simulation purposes


Quote
On Tue, 09 Sep 1997 12:35:11 GMT, E.C.vEer...@KUB.NL (S108502) wrote:
>not the procedure 'random' or 'rand' in TurboPascal, because the generator in
>TurboPascal is flawed.

In what way do you find the random function flawed?

I was a judge at a US state science fair last year, and one of the
students survey random functions for a number of development systems
and determined that TP random was the "best".  (By his definition,
"best" meant richness of results rather than some Chi-square test.)

Re:random number generator for simulation purposes


Quote
In article <5v3fq0$6h...@mailnews.kub.nl>, E.C.vEer...@KUB.NL (S108502) wrote:

]-I'm looking for pointers to random number generators (which are
]-"relatively easy" programmable in TurboPascal).

try fsUltra => this is the Marsaglia PRNG, with code written by Zaman and
Marsaglia of Florida State University.  You should be able to find a copy
at ftp://ftp.coast.net/pub/Coast/disk2/msdos/math/fsultra1.zip

]-not the procedure 'random' or 'rand' in TurboPascal, because the generator in
]-TurboPascal is flawed.

in what way do you find it deficient?

Mark Vaughan

Re:random number generator for simulation purposes


Quote
J. W. Rider wrote:
> >TurboPascal is flawed.
> In what way do you find the random function flawed?

I read somewhere that the random function for Turbo Pascal is seeded
with the system time. Therefore one would think that predictable results
might be, well, predicted.

--
Artificially Intelligent Brain v0.913
http://www.geocities.com/CapeCanaveral/Lab/7677/index.html
aibr...@usa.net -The Only ENLKBB!
Warning: No junk email! Spammers will be auto-spammed with
their own mail, totally ignored, or both.

        "Assimilation is futile. You will be resisted."
                -The H Factory hfact...@usa.net

Re:random number generator for simulation purposes


Quote
AIBrain <aibr...@usa.net> wrote:
>I read somewhere that the random function for Turbo Pascal is seeded
>with the system time. Therefore one would think that predictable results
>might be, well, predicted.

The seeding is done by the procedure Randomize. You can write your
own Randomize procedure, assigning an arbitrary value to RanSeed.

Re:random number generator for simulation purposes


Quote
>Subject: Re: random number generator for simulation purposes
>From: AIBrain <aibr...@usa.net>
>Date: Thu, 11 Sep 1997 02:09:24 -0700
>Message-id: <3417B544.2F1F4...@usa.net>

>J. W. Rider wrote:
>> >TurboPascal is flawed.
>> In what way do you find the random function flawed?

>I read somewhere that the random function for Turbo Pascal is seeded
>with the system time. Therefore one would think that predictable results
>might be, well, predicted.

True, but the system time is usually in terms of approximately 1/18-ths of
a second, so I would think that it is extremely unlikely that someone would
run the program exactly the same time and get the same sequence of random
numbers.

Re:random number generator for simulation purposes


In article <19970911210701.RAA26...@ladder02.news.aol.com> of Thu, 11
Sep 1997 21:07:15 in comp.lang.pascal.borland, CBongChan

Quote
<cbongc...@aol.com> wrote:

>>I read somewhere that the random function for Turbo Pascal is seeded
>>with the system time. Therefore one would think that predictable results
>>might be, well, predicted.

>True, but the system time is usually in terms of approximately 1/18-ths of
>a second, so I would think that it is extremely unlikely that someone would
>run the program exactly the same time and get the same sequence of random
>numbers.

Indeed; and the time is scrambled before being used as RandSeed.

There is one possible trap : if the program, or a part of it, is
automatically started at a certain time each day, one will get the same
random sequence.  A fix for this would be to add the full date, or
date+time, to RandSeed before its first use - this can be obtained as a
longint from the timestamp of a new file; or to add the contents of one
of the other timing registers.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)

Re:random number generator for simulation purposes


In article <5v3fq0$6h...@mailnews.kub.nl>, E.C.vEer...@KUB.NL
says...

Quote

>Good afternoon everybody,

>I'm looking for pointers to random number generators (which are
>"relatively easy" programmable in TurboPascal). And I mean GOOD
random number,

>not the procedure 'random' or 'rand' in TurboPascal, because the
generator in
>TurboPascal is flawed. If anyone could give me any hints,
pointers, book
>titles, references to articles or whatever I'd be most

delighted. I've been

Quote
>searching the WWW and God knows what else, but there doesn't
seem to be
>anything I can use.

>Any help would be extremely appreciated (I'm aware of the fact
that this
>message could have been posted as well have to comp.simulation,
but that
>newsgroup contained 0.0 messages, from which I inferred that no
one reads that

>newsgroup).

>Regards,

>Casper van Eersel
>E.C.vEer...@kub.nl

The problem is that most random number generators do not produce
a completely even distribution of numbers, but tend to be biased
to a particular range (ie they produce more numbers in the range
0.4-0.5 than in the range 0.7-0.8 say). This is a problem for
simulation modelling, because it can bias the results.

I got the following random number generator from a fairly recent
book called something like "Random number generators and Monte
Carlo methods in computer simulation". Use the keywords "Monte
Carlo Random" in a search and you should pick it up. It
apparently produces an extremely flat distribution of results.
The following code is in Visual Basic for Applications, but you
should be able to convert it to pascal pretty easily (the numbers
are IMPORTANT):

Sub TrueRandomize()

{ Just seeds the random number generator with the current date }
{ and time }

  CurrentTime = Time
  CurrentDate = Date
  IX = Year(CurrentDate) * Hour(CurrentTime)
  IY = Month(CurrentDate) * Minute(CurrentTime)
  IZ = Day(CurrentDate) * Second(CurrentTime)
End Sub

Function TrueRandom() As Single

{ returns a random number between 0 and 1 }

  If IsEmpty(IX) Or IsEmpty(IY) Or IsEmpty(IZ) Then TrueRandomize
  IX = (171 * (IX Mod 177)) - (2 * (IX / 177))
  IY = (172 * (IY Mod 176)) - (35 * (IY / 176))
  IZ = (170 * (IZ Mod 178)) - (63 * (IZ / 178))
  If IX < 0 Then IX = IX + 30269
  If IY < 0 Then IY = IY + 30307
  If IZ < 0 Then IZ = IZ + 30323
  TheAnswer = ((IX / 30269) + (IY / 30307) + (IZ / 30323))
  TrueRandom = TheAnswer - Fix(TheAnswer) { returns only the }
                                          { real part of the }
                                          { result           }
End Function

Hope that helps.

Douglas

doug...@liv.ac.uk

Re:random number generator for simulation purposes


Quote
> There is one possible trap : if the program, or a part of it, is
> automatically started at a certain time each day, one will get the
> random sequence.  A fix for this would be to add the full date, or
> date+time, to RandSeed before its first use - this can be obtained as
> longint from the timestamp of a new file; or to add the contents of
> of the other timing registers.

        the program being run at the exact time every day wouldn't necessarily
mean that the numbers generated would be predictable, the computer
doesn't run each procedure in the same amount of time every single
excecution. things such as instruction scheduling and cache waits
interupt the program to a certain extent, as do timer and keyboard
interrupts, etc.
        what i usually do in assembler programs, and this can be easily ported
to pascal, is i start off with a word variable named SEED, this variable
can start of with any value. then when i need a random number, i read a
byte from port 40h (system timer) into AL register. i then subtract SEED
from AX register and then add AX to SEED. here's an example:

program random_num;
uses crt;

var seed : word;
begin;
  repeat
    asm;
      in   al, 40h
      sub  ax, seed
      add  seed, ax
    end;
    write(seed,'...');
  until keypressed;
end.

-shaythestou

Re:random number generator for simulation purposes


Quote
>Subject: Re: random number generator for simulation purposes
>From: shaythestou <shaythes...@cryogen.com>
>Date: Sat, Nov 15, 1997 14:00 EST
>Message-id: <346DF14C.5...@cryogen.com>

>> There is one possible trap : if the program, or a part of it, is
>> automatically started at a certain time each day, one will get the
>> random sequence.  A fix for this would be to add the full date, or
>> date+time, to RandSeed before its first use - this can be obtained as
>> longint from the timestamp of a new file; or to add the contents of
>> of the other timing registers.

>    the program being run at the exact time every day wouldn't necessarily
>mean that the numbers generated would be predictable, the computer
>doesn't run each procedure in the same amount of time every single
>excecution. things such as instruction scheduling and cache waits
>interupt the program to a certain extent, as do timer and keyboard
>interrupts, etc.

True, but I still think that, to be on the safe side, the random # generator
 should be randomized with both date and time.

Quote
>    what i usually do in assembler programs, and this can be easily ported
>to pascal, is i start off with a word variable named SEED, this variable
>can start of with any value. then when i need a random number, i read a
>byte from port 40h (system timer) into AL register. i then subtract SEED
>from AX register and then add AX to SEED. here's an example:

If you're going to generate more than one random # in a row using your method,
 you're going to get in trouble.  The timer increments sequentially and only
 does so with a freq of about 1.183 Mhz.

- Show quoted text -

Quote

>program random_num;
>uses crt;

>var seed : word;
>begin;
>  repeat
>    asm;
>      in   al, 40h
>      sub  ax, seed
>      add  seed, ax
>    end;
>    write(seed,'...');
>  until keypressed;
>end.

>-shaythestou

Other Threads