Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Random number function

0 views
Skip to first unread message

Susie Faux Pas

unread,
Feb 6, 2002, 6:29:24 AM2/6/02
to
I am planning to do some simultaneous growth simulation of several plants.
Each plant are running on its own thread. Can I call the Random function
from within the thread to drive the random growth rate?


Susie Faux Pas

unread,
Feb 6, 2002, 6:54:02 AM2/6/02
to

"Henrick Hellström" <hen...@streamsec.se> wrote in message
news:3C6119B8...@streamsec.se...
> The only thread related thing that might happen afaik is that you
> occasionally (but probably very seldom) get random numbers generated
> from the same seed twice in different threads. It shouldn't matter
> unless you need a very reliable random source with uniform distribution.

Thanks for this info. This is all I need. My simulations doesn't care if the
'randomness' can be repeated or not.

Henrick Hellström

unread,
Feb 6, 2002, 6:55:36 AM2/6/02
to

The only thread related thing that might happen afaik is that you

occasionally (but probably very seldom) get random numbers generated
from the same seed twice in different threads. It shouldn't matter
unless you need a very reliable random source with uniform distribution.

Using a cryptographic random generator might be your best option if your
simulation is to meet scientific standards. The cheapest reliable true
random number generator would be a cheap microphone you plug into the
sound card of your computer. Use the compression algorithm of a hash
function (e.g. SHA) to reduce the white noise in such case. I think
either the multimedia or the winapi group would be the best place to ask
for instructions on how to record from the sound card as safely and
efficiently as possible.

Martin Waldenburg

unread,
Feb 6, 2002, 6:53:24 AM2/6/02
to

Henrick Hellström

unread,
Feb 6, 2002, 7:49:06 AM2/6/02
to

Perhaps, but you should be aware that using Random() in simulations
where the "wrong" arithmetic calculations are involved might screw up
your results completely. Just for fun, you might want to try this code
and perhaps be surprised:

{$Q-,R-}
procedure TForm1.Button1Click(Sender: TObject);
const
ResStr = 'Equal: %d - Unequal: %d';
var
I: Integer;
L, P: LongInt;
Freq: array [Boolean] of Cardinal;
begin
Freq[True] := 0;
Freq[False] := 0;
P := Random($80000000);
for I := 0 to 65535 do begin
P := (2*P + 1) * $08088405;
P := (P + 1) div 2;
L := Random($80000000);
Inc(Freq[P = L]);
P := L;
end;
Memo1.Lines.Add(Format(ResStr,[Freq[True],Freq[False]]))
end;


Sue D. Nom

unread,
Feb 6, 2002, 1:03:04 PM2/6/02
to
<CURIOUS>
Why would the growth rate be random?
</CURIOUS>

Patrick Casey

unread,
Feb 6, 2002, 3:36:11 PM2/6/02
to

One thing you have to be aware of if you're doing serious statistical or
scientific work, is that the random number functions can occasionally be
pushed into degenerate states. This was far more common in 16 bit machines,
but its still a theoretical possibility on a 32 bit system.

As a result, especially if you run your random # sequence out far enough
e.g. generate 2 ^32 pseudo-random numbers from each of the possible 2^32
random number seeds, you will find that your random number sequences hav a
tendency to converge and as a result, you will eventually have < 2^32 unique
numeric streams.

Frankly, for *most* work this is largely academic e.g. the sort of thing
computer scientists talk about after several beers. For some serious
scientific work though (or cryptography of course), it may be something to
consider.

--- Pat

"Susie Faux Pas" <som...@microsoft.server.com> wrote in message
news:3c611385_2@dnews...

Dr John Stockton

unread,
Feb 6, 2002, 2:06:45 PM2/6/02
to
JRS: In article <3c611385_2@dnews>, seen in news:borland.public.delphi.o
bjectpascal, Susie Faux Pas <som...@microsoft.server.com> wrote at Wed,
6 Feb 2002 03:29:24 :-

>I am planning to do some simultaneous growth simulation of several plants.
>Each plant are running on its own thread. Can I call the Random function
>from within the thread to drive the random growth rate?

I have read that :
Random implementation in Delphi (and Borland Pascal) is as follows:
Pseudorandom longint sequence is implemented as a congruential
generator with the formula X[n+1]=134775813*X[n] + 1 (mod 2^32) .
It can be shown that the sequence has full period (its length is 2^32).

One can therefore implement the function using a different "RandSeed" in
each thread, if that is beneficial.

For test purposes you probably should have that capability anyway, so
that tests can use reproducible randoms.

Has anyone a *known-good* expression line the above, but mod 48 or mod
64?

My pas-rand.htm refers.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
<URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Henrick Hellström

unread,
Feb 6, 2002, 7:55:03 PM2/6/02
to
Dr John Stockton wrote:
> Has anyone a *known-good* expression line the above, but mod 48 or mod
> 64?
>
> My pas-rand.htm refers.

Do you mean 48 and 64 or 2^48 and 2^64?

Patrick Casey

unread,
Feb 6, 2002, 4:36:28 PM2/6/02
to

Reference Dr. Stockton's post above. Evidently the Delphi random
implentation is not prone to this behavior e.g. if it has a full period
different seeds will never converge, they'll just track each other through
the sequence.

So you can ignore my warning about convergence.

"Patrick Casey" <pca...@earthlink.net> wrote in message
news:3c61c086_1@dnews...

Dr John Stockton

unread,
Feb 7, 2002, 9:03:45 AM2/7/02
to
JRS: In article <3C61D067...@streamsec.se>, seen in news:borland.p
ublic.delphi.objectpascal, Henrick Hellström <hen...@streamsec.se>
wrote at Thu, 7 Feb 2002 01:55:03 :-

Either would be of interest, but I actually meant the latter. :-(

To reinforce someone else's point : IIRC, I have actually run the
expression quoted and the Delphi RNG (watching RandSeed) in parallel for
a full cycle of 2^32 - they match, and repeat, as expected. But, in
case I remember incorrectly, others can try it.

As one can see from the function, seed 0 is succeeded by 1. ISTM that
for 2^32 truly random 32-bit numbers, there should be approximately one
case of N+1 following N; but the chances of N=0 should be 1 in 2^32.
This seems to be an imperfection of the generator. I've not looked for
a N, N-1 sequence.

Henrick Hellström

unread,
Feb 7, 2002, 2:39:38 PM2/7/02
to
Dr John Stockton wrote:
> JRS: In article <3C61D067...@streamsec.se>, seen in news:borland.p
> ublic.delphi.objectpascal, Henrick Hellström <hen...@streamsec.se>
> wrote at Thu, 7 Feb 2002 01:55:03 :-
>
>>Dr John Stockton wrote:
>>
>>>Has anyone a *known-good* expression line the above, but mod 48 or mod
>>>64?
>>>
>>>My pas-rand.htm refers.
>>>
>>Do you mean 48 and 64 or 2^48 and 2^64?
>>
>
> Either would be of interest, but I actually meant the latter. :-(

Well, if my intuition serves me and if the base is B = 2^(2n) I suspect
that it would be sufficient to select a coefficient c such that c = 1
(mod 4). At least this is true for B = 256, and I'm sure you are able to
prove or disprove my assumption yourself. :-)


> To reinforce someone else's point : IIRC, I have actually run the
> expression quoted and the Delphi RNG (watching RandSeed) in parallel for
> a full cycle of 2^32 - they match, and repeat, as expected. But, in
> case I remember incorrectly, others can try it.
>
> As one can see from the function, seed 0 is succeeded by 1. ISTM that
> for 2^32 truly random 32-bit numbers, there should be approximately one
> case of N+1 following N; but the chances of N=0 should be 1 in 2^32.
> This seems to be an imperfection of the generator. I've not looked for
> a N, N-1 sequence.

An alternative might be to use a 63 bit seed (for B = 2^32) and set:

a := 2*(Seed shr 32) + 1;
s := Seed and $ffffffff;
g := c * (a^-1) (mod B)

s(0,s,g,a) := s;
...
s(i,s,g,a) := g*s(i-1,s,g,a) + a;
...

The sequence (s(i,a*s (mod B),g,a)) would be full length if the sequence
(s(i,s,c,1)) is full length.

Hagen Reddmann

unread,
Feb 7, 2002, 2:34:28 PM2/7/02
to
Hm, on my knownledge LCG's have only on certain seeds the full period. I
think I read about this in Menezes "Handbook of applied Cryptography" or it
was Schneiers book.

Another fast RNG alternative are a LFSR, eg linear feedback shift register.
The Delphi Encryption Compendium, short DEC, contains a fast implementation
with variable periods upto 2^2032-1. This is astronomical. There exists a
special optimized LFSR with Period 2^128-1 that runs at 40 MByte/sec.

Hagen

"Dr John Stockton" <sp...@merlyn.demon.co.uk> schrieb im Newsbeitrag
news:L$zOjCEF7...@merlyn.demon.co.uk...

Lord Crc

unread,
Feb 8, 2002, 4:05:48 AM2/8/02
to

The random function is not thread safe, so control access to it.

- Asbjørn

Dr John Stockton

unread,
Feb 8, 2002, 8:15:05 AM2/8/02
to
JRS: In article <3C62D7F...@streamsec.se>, seen in news:borland.pub

lic.delphi.objectpascal, Henrick Hellström <hen...@streamsec.se> wrote
at Thu, 7 Feb 2002 20:39:38 :-

>Dr John Stockton wrote:
>> JRS: In article <3C61D067...@streamsec.se>, seen in news:borland.p
>> ublic.delphi.objectpascal, Henrick Hellström <hen...@streamsec.se>
>> wrote at Thu, 7 Feb 2002 01:55:03 :-
>>>Dr John Stockton wrote:
>>>
>>>>Has anyone a *known-good* expression line the above, but mod 48 or mod
>>>>64?
>>>>
>>>>My pas-rand.htm refers.
>>>>
>>>Do you mean 48 and 64 or 2^48 and 2^64?
>>
>> Either would be of interest, but I actually meant the latter. :-(
>
>Well, if my intuition serves me and if the base is B = 2^(2n) I suspect
>that it would be sufficient to select a coefficient c such that c = 1
>(mod 4). At least this is true for B = 256, and I'm sure you are able to
>prove or disprove my assumption yourself. :-)

For c=1, c mod 4 = 1 is true. Indeed, c=1 generates a full sequence,
but in perfect order. Or perhaps I have misunderstood you.

A good sequence will be a full sequence, with no discernible order.
Proving no discernible order is, AIUI, a high-grade problem; though
disproving it in a given case may be easy.

Dr John Stockton

unread,
Feb 8, 2002, 8:09:48 AM2/8/02
to
JRS: In article <3c62d7f8$1_1@dnews>, seen in news:borland.public.delph
i.objectpascal, Hagen Reddmann <HaRed...@T-Online.de> wrote at Thu, 7
Feb 2002 20:34:28 :-

>Hm, on my knownledge LCG's have only on certain seeds the full period. I
>think I read about this in Menezes "Handbook of applied Cryptography" or it
>was Schneiers book.

So I believe; and not all full-period ones "look" random.

>Another fast RNG alternative are a LFSR, eg linear feedback shift register.
>The Delphi Encryption Compendium, short DEC, contains a fast implementation
>with variable periods upto 2^2032-1. This is astronomical. There exists a
>special optimized LFSR with Period 2^128-1 that runs at 40 MByte/sec.

Many years ago, a temporary colleague was bouncing such sequences
off the Moon.

he feedback must be carefully selected. With a period of 2^n-1, I don't
know a good way to transform into a Random(m) as in Delphi which is
nominally perfect - though for m << 2^n near-perfection should be easy
enough.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk / JR.St...@physics.org ©
Web <URL: http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

Henrick Hellström

unread,
Feb 8, 2002, 2:13:12 PM2/8/02
to
Dr John Stockton wrote:
>>Well, if my intuition serves me and if the base is B = 2^(2n) I suspect
>>that it would be sufficient to select a coefficient c such that c = 1
>>(mod 4). At least this is true for B = 256, and I'm sure you are able to
>>prove or disprove my assumption yourself. :-)
>>
>
> For c=1, c mod 4 = 1 is true. Indeed, c=1 generates a full sequence,
> but in perfect order. Or perhaps I have misunderstood you.

All c such that c = 1 (mod 4) generates a full sequence for B = 256.
Here is a test procedure:

procedure TForm1.Button1Click(Sender: TObject);
var
I, J, K: Integer;
B, C: Word;
S: string;
NoBreaks: Boolean;
begin
for J := 0 to 31 do begin
C := 4 * J + 1;
for I := 0 to 255 do begin
B := I;
for K := 0 to 254 do begin
B := B * C + 1;
if B and $FF = I then begin
Memo1.Lines.Add(Format('%d - %d - %d',[I,C,K]));
Break;
end;
end;
end;
end;
end;

> A good sequence will be a full sequence, with no discernible order.
> Proving no discernible order is, AIUI, a high-grade problem; though
> disproving it in a given case may be easy.

Worse: It is impossible to prove "no discernible order" because the
sequence generated by any congruential prng *is* ordered in an easily
detectible way.

Sven Pran

unread,
Feb 9, 2002, 5:38:55 PM2/9/02
to

"Henrick Hellström" <hen...@streamsec.se> wrote in message
news:3C642348...@streamsec.se...

> Dr John Stockton wrote:
> >>Well, if my intuition serves me and if the base is B = 2^(2n) I suspect
> >>that it would be sufficient to select a coefficient c such that c = 1
> >>(mod 4). At least this is true for B = 256, and I'm sure you are able to
> >>prove or disprove my assumption yourself. :-)
> >>

A good reference is Donald E Knuth: The Art of Computer Programming, vol 2

The Linear Congruential Method: X := (A*X + C) mod M; has a maximum cycle
length equal to M if and only if:

- C is relatively prime to M
- (A-1) is a multiple of P for any prime number P dividing M
- (A-1) is a multiple of 4 if M is a multiple of 4

These conditions are met by the Delphi PRNG.

In addition it can be shown that for the theoretical statistics to not risk
deviating from
true random with more than q% probability any single application must not
use more
than q% of the total cycle length, that is you must not make more than
q*M/100
calls to RANDOM.

And in order to avoid "serial correlation" in the string of values returned
by the PRNG
it is essential that the multiplier A is between N and M/N where N is the
number of
distinct values requested from the PRNG, that is your Delphi call is to
RANDOM(N).

regards Sven

RB

unread,
Feb 9, 2002, 7:56:09 PM2/9/02
to

"Hagen Reddmann" <HaRed...@T-Online.de> skrev i en meddelelse
news:3c62d7f8$1_1@dnews...

| Hm, on my knownledge LCG's have only on certain seeds the full period.
|

Hi

This can't be true, at least not for sequences generated like

X := (A*X + C) mod M;

If for some seed there is a full period, there wil be no room for other
periods, and
hence there will only be one period.
Different seeds just makes you start different places in the
one and only full period.

RB


Hagen Reddmann

unread,
Feb 10, 2002, 5:58:59 AM2/10/02
to
Yes, read Sven Pran message, he explain it correctly. I only remembered that
for LCG must be some assertitions.


"RB" <rene.de...@NO.SPAM.tdcadsl.dk> schrieb im Newsbeitrag
news:3c65c52b_2@dnews...

0 new messages