Board index » delphi » Random number generator

Random number generator

Quote
Evesham High School wrote:
> When the generator cycles through the random numbers between
> 1 and 49, it often picks out numbers above 49. Why?

{snippet}
     repeat
     gotoxy(x,10);
     for num := 1 to 49 do; {<--Error? Should be} BEGIN
         textcolor(blue);
         textbackground(white);
         write(random(num));
         delay(50);
        {and added} END;
     until keypressed;

Quote
> Ps. How can i stop the generator picking the same number twice without
> setting up different variables for each number generated.

I would add {in pseudocode}
 Var old:integer;
again:
 while num<>old do num:=random
 old:=num
 goto again

--
"To mangle the Turing Test... If ya can't tell, it doesn't matter."
 -a very wise comment from 'Tangent of the Arc' (moc.1cb@llahd)

 

Re:Random number generator


JRS:  In article <OeWlgw1#9GA.153@upnetnews03> of Mon, 19 Oct 1998
13:31:14 in comp.lang.pascal.ansi-iso, Evesham High School
Quote
<evesham_...@msn.com> wrote:
> Help ! Please.
>In the fill Lottery2.pas that is attatched to this message, the scetion of
>code that generates random numbers between 1 and 49 is malfunctioning
>slightly.

>Often it picks numbers that are over 49.

>Can anyone suggest a way to stop this.

>Thanx Michael Carter.   :)

>Ps How do I stop the random number generator picking the same number more
>than once.

>[ A UUEncoded file (lottery.pas) was included here. ]

Please do NOT encode.  It wastes resources and our time.

This is Turbo Pascal, and the correct group is comp.lang.pascal.borland;
where you have earlier posted, and where I have told you where one
"uniqueness" answer is.

Another, inefficient in principle, uniqueness method (which will work)
is to store the "hits" in an array, and scan that to see whether a new
hit is to be accepted.  It would be a better way if drawing, say, two or
three from 1000; you should think why.

Your colours are abominable.

Random(48) should be Random(49) - see manual/help.

I do not see numbers over *48*.

Random()+1 should be Random()+1:2, for neatness.

To c.l.p.a,c.l.p.b, FU c.l.p.b.

--
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.

Other Threads