Board index » delphi » Problems concerning random number generator

Problems concerning random number generator

The for next loop does nothing except setting num to 49.
if you want the indented commands in the loop you'll have to add begin
and end to your code, e.g.

for i := 1 to 49 do
begin
  textbackground();
  textcolor();
end;

Secondly, the reason why sometimes you numbers that are larger than 49
is quite simple really.
If you've just printed for example '39', and in the next loop say '7' is
written on top of that, the number on the screen will show '79'!

Your code must be:

  Write(Random(49):2);

Cheers,
Wouter Ras.

Quote
Evesham High School wrote:

> The program that is attatched to this mesage, lottery2.pas has a few
> bugs that I cannot solve. The FOR ... TO statement that cycles through the
> random numbers between 1 and 49 has a bug, it often brings up numbers that
> are over the maximum value that I stated (which is 49). I can see no errors
> with my code, but I cannot see why the program does this. This is an A-level
> program that is needed for my coursework, and as our national lottery draws
> six numbers that are always below or eqaul to 49, this is somewhat of a
> problem. I would be grateful if you could solve this problem as it would
> greatly enhance my project. Thanks Michael Carter, Evesham High School.

> E-mail- Evesham_...@msn.com

> P.S. Also is there any way that I can stop the random number generator from
> selecting the same number twice without setting up six variable statements.

>                    Name: Lottery2.pas
>     Part 1.2       Type: unspecified type (application/octet-stream)
>                Encoding: x-uuencode

--
Avio
 

Re:Problems concerning random number generator


In article <36275A5B.5...@dutccis.ct.tudelft.nl>,
Wouter Ras  <r...@dutccis.ct.tudelft.nl> wrote:

Quote
>The for next loop does nothing except setting num to 49.

No no no. The value of the loop control variable is undefined outside
the loop.

Quote

>Secondly, the reason why sometimes you numbers that are larger than 49
>is quite simple really.
>If you've just printed for example '39', and in the next loop say '7' is
>written on top of that, the number on the screen will show '79'!

>Your code must be:

>  Write(Random(49):2);

No, one must use a field larger than the largest number. That is at
least 3.

Osmo

Re:Problems concerning random number generator


In article <uuJFPsQ#9GA....@upnetnews02.moswest.msn.net>,
Evesham High School <evesham_...@msn.com> wrote:

Quote
>The program that is attatched to this mesage, lottery2.pas has a few
>bugs that I cannot solve. The FOR ... TO statement that cycles through the
>random numbers between 1 and 49 has a bug, it often brings up numbers that
>are over the maximum value that I stated (which is 49). I can see no errors
>with my code, but I cannot see why the program does this. This is an A-level
>program that is needed for my coursework, and as our national lottery draws
>six numbers that are always below or eqaul to 49, this is somewhat of a
>problem. I would be grateful if you could solve this problem as it would
>greatly enhance my project. Thanks Michael Carter, Evesham High School.

>E-mail- Evesham_...@msn.com

>P.S. Also is there any way that I can stop the random number generator from
>selecting the same number twice without setting up six variable statements.

>begin 666 Lottery2.pas
>M<')O9W)A;2!F87-T7VQO='0[#0H-"G5S97,@8W)T.PT*#0IV87(-"@T*(" @
>M>#H@:6YT96=E<CL-"B @(&YU;3H@:6YT96=E<CL-"B @(&XZ(&EN=&5G97([

Are you trolling? First do not uuencode the source you send. That makes
reading it hard.  

As for the actual code, it is almost unreadable because of your strange
dendentations:

procedure lott;

begin
     gotoxy(2,5);
     writeln('Press enter to select ball no ', n,': ');
     randomize;
     repeat
     gotoxy(x,10);
     for num := 1 to 49 do;
         textcolor(blue);
         textbackground(white);
         write(random(num));
         delay(50);
     until keypressed;

Now where does that repeat until loop begin? The indentations say at the for
line, but the repeat is actually further up. Second the for loop is just
an empty loop as there is semicolon after the do. Also as num is
undefined after the loop you can get strange values.

Also I do not see what is the point of the loop anyway? Are you drawing
49 numbers?

The way to do is to use Random(49)+1 to select the value. Then you have
to somehow prevent duplicates, like keeping a set of either available or
already selected numbers.

     n:= n + 1;
     x:= x + 7;
     textcolor(white);
     readln;
     textcolor(white);
     textbackground(blue);
end;

Osmo

Re:Problems concerning random number generator


In article <uuJFPsQ#9GA....@upnetnews02.moswest.msn.net>,
Evesham High School <evesham_...@msn.com> wrote:

:random numbers between 1 and 49 has a bug, it often brings up numbers that
:are over the maximum value that I stated (which is 49). I can see no errors

 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

:begin 666 Lottery2.pas
:M<')O9W)A;2!F87-T7VQO='0[#0H-"G5S97,@8W)T.PT*#0IV87(-"@T*(" @

DON'T post in a binary format in a discussion newsgroup, whatever
format the actual contents!

   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 concerning random number generator


hello
here is my solution (should work(s))

program fast_lott;

uses crt;

var

   x: integer;
   num: integer;
   n: integer;
   count: integer;
   tot: integer;
   continue: string;
   ntira : integer;
   ttira : array[1..6] of integer;

procedure init;

          begin
          x:= 20;
          n:= 1;
          count:= 1;
          tot:=1;
          end;

procedure title;

          begin

          clrscr;
          textcolor(white);
          textbackground(blue);
          clrscr;
          gotoxy(25,3);
          writeln('* Lottery number generator *');
          gotoxy(25,4);
          writeln('----------------------------');
          gotoxy(8,6);
          writeln('Lottery number generator v1.0 by Michael Carter,
September 1998');
          gotoxy(25,20);
          write('Please press <enter> to continue: ');
          textcolor(blue); readln(continue); textcolor(white);
          if continue = 'manager' then halt;
          end;

procedure start;

begin

     clrscr;
     gotoxy(2,5);
     write('Press enter to start the game: ');
     readln;
     clrscr;
end;

procedure lott;
var
  stira : string[2];
  tira, i : integer;
  bool : boolean;
begin
     gotoxy(2,5);
     writeln('Press enter to select ball no ', n,': ');
     randomize;
     repeat
       gotoxy(x,10);
       textcolor(blue);
       textbackground(white);
       bool := false;
       tira := random(49) + 1;
       for i := 1 to ntira do
       begin
         if ( tira = ttira[i] ) then
              bool := true;
       end;
       if ( not bool ) then
       begin
         str(tira:2, stira);
         write(stira);
         delay(50);
       end;
     until ( (not bool) and (keypressed));
     inc(ntira);
     ttira[ntira] := tira;
     n:= n + 1;
     x:= x + 7;
     textcolor(white);
     readln;
     textcolor(white);
     textbackground(blue);
end;

procedure ending;

          begin
          gotoxy(40,13); writeln('^'); gotoxy(40,14); writeln('^');
          gotoxy(20,15);
          writeln('Here are your lottery numbers for this time');
          gotoxy(30,17);
          write('Press <enter> to continue: ');
          readln;
          clrscr;
          end;

procedure rep;
var
  i : integer;
begin

     title;
     init;
     start;
     for i := 1 to 6 do
         ttira[i] := -1;
     ntira := 0;
     repeat
       lott;
       inc(tot);
     until (tot=7);
     ending;
end;

{************ Main program *************}

begin
     while continue <> 'manager' do
     rep;

end.

bye
patrick
marseille
Evesham High School a crit dans le message ...

Quote
>The program that is attatched to this mesage, lottery2.pas has a few
>bugs that I cannot solve. The FOR ... TO statement that cycles through the
>random numbers between 1 and 49 has a bug, it often brings up numbers that
>are over the maximum value that I stated (which is 49). I can see no errors
>with my code, but I cannot see why the program does this. This is an
A-level
>program that is needed for my coursework, and as our national lottery draws
>six numbers that are always below or eqaul to 49, this is somewhat of a
>problem. I would be grateful if you could solve this problem as it would
>greatly enhance my project. Thanks Michael Carter, Evesham High School.

>E-mail- Evesham_...@msn.com

>P.S. Also is there any way that I can stop the random number generator from
>selecting the same number twice without setting up six variable statements.

Re:Problems concerning random number generator


Quote
Osmo Ronkanen wrote:

> In article <36275A5B.5...@dutccis.ct.tudelft.nl>,
> Wouter Ras  <r...@dutccis.ct.tudelft.nl> wrote:
> >The for next loop does nothing except setting num to 49.

> No no no. The value of the loop control variable is undefined outside
> the loop.

This is not true. See how Turbo Pascal compiles this procedure:

procedure loop;
var i:integer;
begin
  for i := 1 to 49 do
    write;
  writeln(i);
end;

15BB:0015 FF46FE        INC     WORD PTR [BP-02]
15BB:0018 BF5401        MOV     DI,0154
15BB:001B 1E            PUSH    DS
15BB:001C 57            PUSH    DI
15BB:001D 9AFE05C615    CALL    15C6:05FE
15BB:0022 9A9102C615    CALL    15C6:0291
15BB:0027 837EFE31      CMP     WORD PTR [BP-02],+31
15BB:002B 75E8          JNZ     0015
15BB:002D BF5401        MOV     DI,0154
15BB:0030 1E            PUSH    DS
15BB:0031 57            PUSH    DI
15BB:0032 8B46FE        MOV     AX,[BP-02]
15BB:0035 99            CWD
15BB:0036 52            PUSH    DX
15BB:0037 50            PUSH    AX
15BB:0038 31C0          XOR     AX,AX
15BB:003A 50            PUSH    AX
15BB:003B 9A9106C615    CALL    15C6:0691

As you clearly can see, the value of i [BP-02] is *not* undefined after
the loop.

Quote

> >Secondly, the reason why sometimes you numbers that are larger than 49
> >is quite simple really.
> >If you've just printed for example '39', and in the next loop say '7' is
> >written on top of that, the number on the screen will show '79'!

> >Your code must be:

> >  Write(Random(49):2);

> No, one must use a field larger than the largest number. That is at
> least 3.

This is also not true.

Quote

> Osmo

--
Wouter Ras.

Re:Problems concerning random number generator


JRS:  In article <uuJFPsQ#9GA....@upnetnews02.moswest.msn.net> of Fri,
16 Oct 1998 14:35:35 in comp.lang.pascal.borland, Evesham High School

Quote
<evesham_...@msn.com> wrote:

(in header)

Quote
>Date: Fri, 16 Oct 1998 14:35:35 +0200

That +0200 puts Evesham on the Continent somewhere, I believe?

Quote
>The program that is attatched to this mesage,

Better not to attach; include (copy'n'paste should do).

Quote
>lottery2.pas has a few
>bugs that I cannot solve. The FOR ... TO statement that cycles through the
>random numbers between 1 and 49 has a bug, it often brings up numbers that
>are over the maximum value that I stated (which is 49). I can see no errors
>with my code, but I cannot see why the program does this. This is an A-level
>program that is needed for my coursework, and as our national lottery draws
>six numbers that are always below or eqaul to 49, this is somewhat of a
>problem. I would be grateful if you could solve this problem as it would
>greatly enhance my project. Thanks Michael Carter, Evesham High School.

>E-mail- Evesham_...@msn.com

QHAH.

Quote
>     for num := 1 to 49 do;

Replace ";" with " begin"

Quote
>         textcolor(blue);
>         textbackground(white);
>         write(random(num));
>         delay(50);
Insert "end;"
>     until keypressed;

Those changes are I think needed to do what you intended; without them
the "for" loop is futile.

But I don't think you want that loop, and I think you do want to use
Random(49)+1.

Quote
>P.S. Also is there any way that I can stop the random number generator from
>selecting the same number twice without setting up six variable statements.

Simplest to declare an array [1..49], fill with numbers 1..49, select
one of 49 at random, move top one to selected place, select one of 48,
...  Like shuffling cards, but Move instead of Swap - see my Web <URL:
http://www.merlyn.demon.co.uk/pascal.htm#Rand>.

--
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 concerning random number generator


In article <36279517.1...@dutccis.ct.tudelft.nl>,
Wouter Ras  <r...@dutccis.ct.tudelft.nl> wrote:

Quote
>Osmo Ronkanen wrote:

>> In article <36275A5B.5...@dutccis.ct.tudelft.nl>,
>> Wouter Ras  <r...@dutccis.ct.tudelft.nl> wrote:
>> >The for next loop does nothing except setting num to 49.

>> No no no. The value of the loop control variable is undefined outside
>> the loop.

>This is not true. See how Turbo Pascal compiles this procedure:

Yes it is. The fact that you can show one compiler that produces
certain code does not change it.
...

Quote

>> >Secondly, the reason why sometimes you numbers that are larger than 49
>> >is quite simple really.
>> >If you've just printed for example '39', and in the next loop say '7' is
>> >written on top of that, the number on the screen will show '79'!

>> >Your code must be:

>> >  Write(Random(49):2);

>> No, one must use a field larger than the largest number. That is at
>> least 3.

>This is also not true.

If you want the output to be readable it has to be.

Osmo

Re:Problems concerning random number generator


Quote
Osmo Ronkanen wrote:
> Yes it is. The fact that you can show one compiler that produces
> certain code does not change it.
> ...

There is no reason why a value should suddenly vanish when a for..next
loop ends. Assembler doesn't have loops, so there's no way of knowing
for it that a loop has ended. The variable keeps its value until the
procedure has ended, because the allocated stack memory is then
released.
So it is perfectly legal to assume that the loop variable keeps its
latest value it had during the loop.

Quote
> If you want the output to be readable it has to be.

This is completely different from what you stated before. You said it
*has* to be one field larger than the largest number, without further
explaining why this is.
If you would have taken one minute to look at the code with which this
discussion started, you would have seen that in this case the matter of
readable output is not an issue.

Quote

> Osmo

--
Avio

Re:Problems concerning random number generator


In article <36284452.4...@dutccis.ct.tudelft.nl>,
Wouter Ras  <r...@dutccis.ct.tudelft.nl> wrote:

Quote
>Osmo Ronkanen wrote:

>> Yes it is. The fact that you can show one compiler that produces
>> certain code does not change it.
>> ...

>There is no reason why a value should suddenly vanish when a for..next
>loop ends. Assembler doesn't have loops, so there's no way of knowing
>for it that a loop has ended. The variable keeps its value until the
>procedure has ended, because the allocated stack memory is then
>released.
>So it is perfectly legal to assume that the loop variable keeps its
>latest value it had during the loop.

No it is not. The value undefined. A compiler with different code
generation might for example use a register as lop variable and not put
the value back in the actual position in the memory. the value of loop
control variable is undefined after the loop.

Quote

>> If you want the output to be readable it has to be.

>This is completely different from what you stated before. You said it
>*has* to be one field larger than the largest number, without further
>explaining why this is.

It was obvious from the context. The original version did not have
fixed field and therefore the values were output without no spaces
making it look like a large value. The fix used field 2 when the values
could be as large as 49. I said that one must use a field at least 3.

Quote
>If you would have taken one minute to look at the code with which this
>discussion started, you would have seen that in this case the matter of
>readable output is not an issue.

I looked the code and pointed several errors from it.

Osmo

Re:Problems concerning random number generator


Quote
Osmo Ronkanen wrote:

> No it is not. The value undefined. A compiler with different code
> generation might for example use a register as lop variable and not put
> the value back in the actual position in the memory. the value of loop
> control variable is undefined after the loop.

Fact is, we're not talking about a different compiler where it might or
not might be undefined. This is the group pascal.BORLAND, is it?
If I am correct, what you're saying is that it is not a nice way of
programming. I agree, but that's a different issue. In the end there is
nothing wrong with it, because the loopvalue will be exactly what you
expect it to be after the loop.
Obviously, I cannot convince you. All I can say now is: Why don't you
just try making a program, which reads the loopvalue after the loop is
done. You will find n out of n times that the value is not undefined.

Quote
> It was obvious from the context. The original version did not have
> fixed field and therefore the values were output without no spaces
> making it look like a large value. The fix used field 2 when the values
> could be as large as 49. I said that one must use a field at least 3.

The original version did have gotoxy statements and therefore there were
several spaces between them. So, for Lottery2.pas a width of 2 is
sufficient.

Quote

> >If you would have taken one minute to look at the code with which this
> >discussion started, you would have seen that in this case the matter of
> >readable output is not an issue.

> I looked the code and pointed several errors from it.

Ah yes, after first making a remark about unreadable Michael's code
is... Very nice.

Have a nice life,
Wouter

Re:Problems concerning random number generator


In article <3628AA71.1...@dutccis.ct.tudelft.nl>,
Wouter Ras  <r...@dutccis.ct.tudelft.nl> wrote:

Quote
>Osmo Ronkanen wrote:

>> No it is not. The value undefined. A compiler with different code
>> generation might for example use a register as lop variable and not put
>> the value back in the actual position in the memory. the value of loop
>> control variable is undefined after the loop.

>Fact is, we're not talking about a different compiler where it might or
>not might be undefined. This is the group pascal.BORLAND, is it?

It is undefined even on Borland Pascal. The fact that the compilers
that you have inspected produce specific code does not define the value.
Using such compiler dependent features is very ugly. In some cases there
might be justification in form of speed, but that would be very rare and
does not IMO apply to the case.

Remember that many people use other compilers that are compatible with
TP. Those might half for loop very differently.

Quote
>If I am correct, what you're saying is that it is not a nice way of
>programming. I agree, but that's a different issue.

It is different from not nice, it is wrong.

Quote
> In the end there is
>nothing wrong with it, because the loopvalue will be exactly what you
>expect it to be after the loop.

Have you tested it on all versions of TP? Also if there were a future
version it could handle things differently.

It is just people who use all these undocumented features that are to
blame for all the things that have to be done in new versions to keep
compatibility when it would not normally be needed. For example the
reason why in PCs one has to mess with the A20 line is because some
clever guys decided that it is nice to utilize the fact that the
addresses on 8086 wrap at 1MB. Please stick to the documented features
when that is possible.

Quote
>Obviously, I cannot convince you.

No you cannot. Utilizing such features is asking trouble.

Quote
>All I can say now is: Why don't you
>just try making a program, which reads the loopvalue after the loop is
>done. You will find n out of n times that the value is not undefined.

That does not document it.

Osmo

Re:Problems concerning random number generator


JRS:  In article <36284452.4...@dutccis.ct.tudelft.nl> of Sat, 17 Oct
1998 09:16:34 in comp.lang.pascal.borland, Wouter Ras

Quote
<r...@dutccis.ct.tudelft.nl> wrote:
>Osmo Ronkanen wrote:

>> Yes it is. The fact that you can show one compiler that produces
>> certain code does not change it.
>> ...

>There is no reason why a value should suddenly vanish when a for..next
>loop ends.

The value is DEFINED as being undefined in Pascal.

In some implementations it may consistently have the last wanted value.
In some implementations it may consistently have the first unwanted
value.
In some implementations it may consistently have some other value.
In some implementations it may consistently be apparently random.
In some implementations it may consistently still have the pre-loop
value.
In some implementations it may consistently be inconsistent.

Therefore one should not use it.

It is IMHO a pity that the FOR loop was not designed as
        for J:integer := 1 to 10 do <statement> ;
with J being in scope only during <statement>.

--
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 concerning random number generator


In article <tlulUuAyLQK2E...@merlyn.demon.co.uk>,
Dr John Stockton  <j...@merlyn.demon.co.uk> wrote:

Quote
>It is IMHO a pity that the FOR loop was not designed as
>        for J:integer := 1 to 10 do <statement> ;
>with J being in scope only during <statement>.

Well IMO it would be stupid if one needed to define a type during the
loop. On the other hand it would be nice if it assigned the type
automatically.

Osmo

Re:Problems concerning random number generator


JRS:  In article <70d6tr$...@kruuna.Helsinki.FI> of Sun, 18 Oct 1998
20:01:47 in comp.lang.pascal.borland, Osmo Ronkanen

Quote
<ronka...@cc.helsinki.fi> wrote:
>In article <tlulUuAyLQK2E...@merlyn.demon.co.uk>,
>Dr John Stockton  <j...@merlyn.demon.co.uk> wrote:
>>It is IMHO a pity that the FOR loop was not designed as
>>        for J:integer := 1 to 10 do <statement> ;
>>with J being in scope only during <statement>.

>Well IMO it would be stupid if one needed to define a type during the
>loop. On the other hand it would be nice if it assigned the type
>automatically.

At this time of night, I cannot be sure whether it is always possible
and safe to deduce the type from the limits.  Giving the type explicitly
does provide a check on intentions.

ISTM that it would not be wise to deduce just from limits, since if
<statement> is compound (begin..end) and contains routines with longint
parameters J, it might be better to have J itself longint rather than
byte.

However, there should definitely be a type-ID, since that makes the
declaration manifest, and it would be well to retain "all variables must
be declared" as true.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v4.00    MIME.
 Web <URL: http://www.merlyn.demon.co.uk/> -- includes FAQish topics and links:
 Dates - misctime.htm  Year 2000 - date2000.htm  Critical Dates - critdate.htm
 Y2k for beginners - year2000.txt  UK mini-FAQ - y2k-mfaq.txt  Don't Mail News.

Go to page: [1] [2]

Other Threads