troubleshooting Question

Problems with random

Avatar of jbeh
jbeh asked on
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;
//z := 0;

//timer1.Enabled := false;

cycles := cycles + 1;

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

//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);

bkgrnd.Canvas.Brush.Color := clblue;
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));

sleep (1);

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

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.



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