Board index » delphi » Problems with the random number generator in TP7

Problems with the random number generator in TP7

Can anyone help me please?

I have written a lottery program for my A-Level course, the only problem is
that after I told the code only to pick random numbers between 1 and 49,
sometimes the computer pulls out numbers that are greater than 49.

Can anyone please explain to me, why this does this?

Thanx

Michael Carter :-)

 

Re:Problems with the random number generator in TP7


Hi Michael,

Quote
> I have written a lottery program for my A-Level course, the only problem is
> that after I told the code only to pick random numbers between 1 and 49,
> sometimes the computer pulls out numbers that are greater than 49.

It would be nice if you could say us HOW you told the program to pick random
numbers between 1 and 49. If I tell my programs not to pick any greater
numbers they wouldn't.
Random(49) creates a random value from 0 to 48. So if you use Random(49)+1 it
should really pick values from 1 to 49 - exactly what you wanted. :-)

    Bye,
      Ingo

Re:Problems with the random number generator in TP7


In comp.lang.pascal.borland, Evesham High School spluttered:
Quote

>I have written a lottery program for my A-Level course, the only problem is
>that after I told the code only to pick random numbers between 1 and 49,
>sometimes the computer pulls out numbers that are greater than 49.
>Can anyone please explain to me, why this does this?

It would be helpful if you posted the problematic code - just that bit
for getting a random number. I'm sure somweone will be able to help.

--
Pedt Scragg

Never challenge a porcupine to an ass-kicking contest

Re:Problems with the random number generator in TP7


Quote
>I have written a lottery program for my A-Level course, the only problem is
>that after I told the code only to pick random numbers between 1 and 49,
>sometimes the computer pulls out numbers that are greater than 49.

Which version of TP do you use? V. 6.0 had a bug in the randommer.
Alternatively you could try to use this simple implementation of the TP7-
randommer:

function Random(Range: Word): Word;
begin
  RandSeed := ($8088405 * RandSeed + 1);
  Random := ((RandSeed shr 16) * Range + ((RandSeed and $FFFF) *
              Range shr 16) ) shr 16;
end;

Bye,
Stefan
---
please remove the P in my email-adress to answer me
take a look @ my homepage: http://sourcenet.home.pages.de/

Re:Problems with the random number generator in TP7


JRS:  In article <u9I4kK#C#GA.202@upnetnews03> of Mon, 9 Nov 1998
13:12:19 in news:comp.lang.pascal.borland, Evesham High School

Quote
<evesham_...@msn.com> wrote:
>I have written a lottery program for my A-Level course, the only problem is
>that after I told the code only to pick random numbers between 1 and 49,
>sometimes the computer pulls out numbers that are greater than 49.

In what way were the responses to the rather similar question that you
posted on October 19th insufficient?

If you are doing the UK Lottery, the numbers you want are *between* 0 &
50, being in the range 1..49.  I used to know the man responsible for
checking the balls.

Ball := Random(49)+1 gives what you want, but remember to write Ball
with a fieldwidth of >=2 (if over-writing).
GoTo(X, Y) ; Write('Ball ', Ball:2, ' drawn') ;   is safe.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v4.00    MIME.
  Web <URL: http://www.merlyn.demon.co.uk/> - TP/BP/&c. FAQqish topics & links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.

Re:Problems with the random number generator in TP7


On Mon, 9 Nov 1998 19:58:24 +0100, "Stefan Goehler"

Quote
<stefan.goeh...@gmxP.de> wrote:
> >I have written a lottery program for my A-Level course, the only problem is
> >that after I told the code only to pick random numbers between 1 and 49,
> >sometimes the computer pulls out numbers that are greater than 49.
> Which version of TP do you use? V. 6.0 had a bug in the randommer.

This so-called bug doesn't affect the range of the numbers random(n)
will return. The range will be definitely 0..n-1 in both versions.

Let's restate for correctness' sake the effect of the thoughtless
implementation of random(n) in TP 6.0.

A) It affects the periodicity of Random(n) in some cases.

Any TP 7.0 random(2) sequemce will repeat only after 2^32 ~
4,000,000,000 calls.

A TP 6.0 random(2) sequence (0-1 sequence) will repeat after 2^17 =
2*65536 calls in identical form. A random(4) sequence after 2^18 calls
etc. Any other sequence where n is _not_ a power of two will repeat
only after 2^32 calls like a TP 6.0 sequence.

B) random(n) in TP 6.0 will be mathematically catastrophic - although
this may have no serious impact on less demanding simulations - for
big n. If n is $F000, then on average every value in the range
0..$0FFF will occur twice as often as any value in the range
$1000..$EFFF.

The "unequalness" is only about 65536/65535 in TP 7.0 in the worst
case instead of 2/1 in TP 6.0

Regards
Horst

Re:Problems with the random number generator in TP7


[If two copies of this have shown up, the other one contains a minor error,
which I've corrected here.  Disregard the other, which I'll attempt to cancel
if I see.]

Quote
Stefan Goehler wrote:
> Alternatively you could try to use this simple implementation of the TP7-
> randommer:

Slight addition to avoid potential frustration:

{$IFOPT R+}{$DEFINE ROPT}{$R-}{$ENDIF}

Quote
> function Random(Range: Word): Word;
> begin
>   RandSeed := ($8088405 * RandSeed + 1);
>   Random := ((RandSeed shr 16) * Range + ((RandSeed and $FFFF) *
>               Range shr 16) ) shr 16;
> end;

{$IFDEF ROPT}{$R+}{$UNDEF ROPT}{$ENDIF}

Quote
> Bye,
> Stefan
> ---
> please remove the P in my email-adress to answer me
> take a look @ my homepage: http://sourcenet.home.pages.de/

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |

Re:Problems with the random number generator in TP7


In article <u9I4kK#C#GA.202@upnetnews03>,
Evesham High School <evesham_...@msn.com> wrote:
:I have written a lottery program for my A-Level course, the only problem is
:that after I told the code only to pick random numbers between 1 and 49,
:sometimes the computer pulls out numbers that are greater than 49.
:Can anyone please explain to me, why this does this?

Dear Michael Carter,

Already covered in the FAQ

 139) How do I get a random number from 1 to 49 instead of 0 to 49?

 151578 Oct 10 1998 ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip
 tsfaqp.zip Common Turbo Pascal Questions and Timo's answers, linked

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/>  ; FIN-65101,  Finland

Spam foiling in effect.  My email filter autoresponder will return a
required email password to users not yet in the privileges database.
Advice on spam foiling at http://www.uwasa.fi/~ts/info/spamfoil.html

Re:Problems with the random number generator in TP7


On Tue, 10 Nov 1998 00:03:49 -0500, Scott Earnest

Quote
<setech@_ix.netcom.com> wrote:

> Slight addition to avoid potential frustration:

> {$IFOPT R+}{$DEFINE ROPT}{$R-}{$ENDIF}
> > function Random(Range: Word): Word;
> > begin
> >   RandSeed := ($8088405 * RandSeed + 1);
> >   Random := ((RandSeed shr 16) * Range + ((RandSeed and $FFFF) *
> >               Range shr 16) ) shr 16;
> > end;
> {$IFDEF ROPT}{$R+}{$UNDEF ROPT}{$ENDIF}

Sorry. In the above code there is no case where a range check could
possibly bomb. A range check is applied to array-accesses, assigments
and situations with assigment semantics like passing of value
parameters to function calls, assigning return values to functions -
but never to intermediate results of arithmetic operations (this is
the Q+ domain).

In the first assigment RandSeed is LongInt and for LongInt no range
check ever applies because LongInt can hold any assignable integral
value.

For the second assignment it does apply, but anything shr 16 is
guaranteed to be a number in the range 0..$FFFF which is the range of
word (shr is a logical shift and no arithmetic shift)

You have to turn off {$Q+} (the culprit is $8088405*Randseed) if the
procedure is run with TP 7.0 - but you won't use it then because the
procedure is made for TP 6.0-

Regards
Horst

Re:Problems with the random number generator in TP7


Quote
Horst Kraemer wrote:
> On Tue, 10 Nov 1998 00:03:49 -0500, Scott Earnest
> <setech@_ix.netcom.com> wrote:

> > [code snip]

> Sorry. In the above code there is no case where a range check could
> possibly bomb. A range check is applied to array-accesses, assigments
> and situations with assigment semantics like passing of value
> parameters to function calls, assigning return values to functions -
> but never to intermediate results of arithmetic operations (this is
> the Q+ domain).

Mea culpa here, I stand corrected.  I know TP6 didn't have the $Q directive
that TP7 does, and I was thinking erroneously that the range checking in TP6
also handles range overflow of variables.  A quick test program proved to me
that apparently it doesn't.

Quote
> Regards
> Horst

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |

Other Threads