Solved

random numbers

Posted on 2001-08-15
5
198 Views
Last Modified: 2010-04-06
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 ?
0
Comment
Question by:catbecks
5 Comments
 
LVL 1

Accepted Solution

by:
Phoenix_s earned 100 total points
ID: 6390057
depending on what kind of granularity you require.... the process for generating the timer interval can be done a number of ways

you have a starting point at 12000 so that's a constant

the remainder is 20000 of which you need a randomized chunk



let's assume the startvalue is 12000
also let's assume you want granularity of 500 ticks

then endvalue would be 12000 + ((random(40)+1)*500)

because random(x) provides values from 0 -> (x-1)

we want 1 -> x  so we add 1 to the random before we multiply anything

let's assume you want granularity of 100 ticks

then endvalue would be 12000 + ((random(200)+1)*100)

let's assume you want granularity of 250 ticks

then endvalue would be 12000 + ((random(80)+1)*250)


let's assume you want granularity of 1000 ticks

then endvalue would be 12000 + ((random(20)+1)*1000)


does that help at all
0
 
LVL 4

Expert Comment

by:YodaMage
ID: 6390061
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;

0
 
LVL 6

Expert Comment

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

I hope this helps,

Stu.
0
 
LVL 1

Expert Comment

by:Phoenix_s
ID: 6391421
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
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6391744
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to debug For loops? 3 63
LAN or WAN ? 11 106
TEMBEDDEDWB how can i change its user agent ? 8 85
how to resize animated Gif image in delphi ? 1 45
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question