Board index » delphi » random is not so random in my timer

random is not so random in my timer

procedure TForm1.Timer1Timer(Sender: TObject);
var i,j : integer;
begin
if counter > ax then
        begin
        ...
        counter := 1;
        empty b[];
        and other stuff;
        ...
        end
else
        begin
        randomize;
        b[counter] := random(numberColors);
        drawChip(image2,counter,1,b[counter]);
        inc(counter);
        end;
end;

It starts out fine, but after a short while the randomness disappears and
the numbers come out in sequence (1,2,3,1,2,3,1,2,3).
I first put randomize in the form create, but the same thing happened.

What is going on?

 

Re:random is not so random in my timer


Randomize should only be called once in the program. Remove the call from
your timer.

Re:random is not so random in my timer


You need to call Randomize only once in your app instance. Call it from
FormCreate() or FormActivate() for better results.

Ryan

Quote
>procedure TForm1.Timer1Timer(Sender: TObject);
>var i,j : integer;
>begin
>if counter > ax then
>        begin
>        ...
>        counter := 1;
>        empty b[];
>        and other stuff;
>        ...
>        end
>else
>        begin
>        randomize;
>        b[counter] := random(numberColors);
>        drawChip(image2,counter,1,b[counter]);
>        inc(counter);
>        end;
>end;

>It starts out fine, but after a short while the randomness disappears and
>the numbers come out in sequence (1,2,3,1,2,3,1,2,3).
>I first put randomize in the form create, but the same thing happened.

>What is going on?

Other Threads