troubleshooting Question

Problems with random

Avatar of jbeh
jbeh asked on
Delphi
3 Comments1 Solution427 ViewsLast Modified:
I have the following code in a timer event...

procedure TForm1.Timer1Timer(Sender: TObject);
var y : Integer;
var x : Integer;
begin
//z := 0;

//timer1.Enabled := false;

cycles := cycles + 1;

while z < 2 do begin
x := 100;
//y := 100;
inc (z,1);
end;


//x := x + RandomRange(3, 5);

//x := x + randomrange(-2, 7);
x := x + random(5);

//x := y;

//x := x + randomfrom[-1,-2,0,-1,-2];

if x > mypiccy.Width then begin
y := x   ;
x := x mod bkgrnd.Width;
ListBox1.AddItem(inttostr(cycles)+' - '+inttostr(y)+' - '+inttostr(x),nil);
end;

bkgrnd.Canvas.Brush.Color := clblue;
bkgrnd.Canvas.FillRect(rect(0,0,bkgrnd.Width,bkgrnd.Height));
bkgrnd.Canvas.CopyRect(rect(x,x,mypiccy2.Width + x, mypiccy2.Height + x),mypiccy2.Canvas,rect(0,0,mypiccy2.Width,mypiccy2.Height));
bkgrnd.Canvas.CopyRect(rect(x,x,mypiccy.Width + x, mypiccy.Height + x),mypiccy.Canvas,rect(0,0,mypiccy.Width,mypiccy.Height));

form1.Canvas.CopyRect(rect(0,0,bkgrnd.Width,bkgrnd.Height),bkgrnd.Canvas,rect(0,0,bkgrnd.Width,bkgrnd.Height));
sleep (1);
//inc(x,1);


//if x > bkgrnd.Width then x := 0 ;//showmessage('here');
if x > bkgrnd.height then showmessage(inttostr (x));
//timer1.Enabled := true;
end;




My problem is that something is stomping all over "x" in the above


The first value is 102 - fine but the second and all subsequent are up at about 12656033 / 032 /034 whatever.

I can't see how this is happening.


The basic idea of the proglet is to move an icon randomly round a section of the form canvas.

Can someone explain my error please.

Ta

John
ASKER CERTIFIED SOLUTION
edsteele

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros