Link to home
Start Free TrialLog in
Avatar of catbecks
catbecks

asked on

random numbers

ive got this code in my program
timer4.interval:=4000;
now i want to make the 4000 a random number between
12000 and 32000 how do i do it ?
ASKER CERTIFIED SOLUTION
Avatar of Phoenix_s
Phoenix_s

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YodaMage
YodaMage

var
  test : boolean;
  num  : integer;
begin
  test := false;
  Randomize;
  while not test do
  begin
    num := round((random) * 100000);
    if (num >= 12000) and (num <= 32000) then test := true;
  end;
  timer4.interval:= num;
end;

procedure TForm1.UpdateTimeInterval;
begin
  Randomize;
  Timer4.Interval := 12000 + Random(32000);
end;

I hope this helps,

Stu.
ah hell  if you can do it...

pass parameters to the updatetimeinterval event as below..

procedure tform1.updatetimeinterval(offset,range,granularity:integer);
begin
  randomize;
  timer4.interval := offset + ((random(range div granularity)+1)* granularity);
end;

ex of use

updatetimeinterval(12000,20000,250);  will give a timerinterval of 12000 + 250 -> 20000 in increments of 250ms

or have application-wide or unit-wide variables declared as part of a initialization sequence at startup

offset#,
range#,
granularity# : integer;

you may have multiple timers with different rules so just stick in the appropriate timer's parameters as offset0, range0, and granularity0 for timer0, and so on...

procedure tform1.updatetime#interval;
begin
  randomize;
  timer#.interval := offset# + ((random(range# div granularity#)+1)* granularity#);
end;

replacing the # with timer #'s
Hi catbecks,

here is my version :-)

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  TTimer(Sender).Interval := 12000 + Random(32000-12000);
  //
  // rest of your code here
  //
end;

initialization
  // to avoid of the same sequences on every programm start
  Randomize;
end.