We help IT Professionals succeed at work.

Random numbers

tyfing
tyfing asked
on
Medium Priority
221 Views
Last Modified: 2010-04-06
how do i make the random number appear in editbox1 once i clicked button1 ? the random number must be an integer between -2 to 2, 0 inclusive.
Comment
Watch Question

Commented:
try this one....

OnClick procedure of Button1

var num: integerl;

begin
  rand();
  num := randomize(2);

  edit1.text := inttostr(num);
end;




Commented:
Add somewhere in your project's initialization a call to "Randomize", but only call it once in your program. Thereafter you can anytime do this:

edit1.text := IntToStr(Random(5) - 2);

Regards, Madshi.

Author

Commented:
wat does the 5 and 2 mean ?

Commented:
The parameter you give in to Random defines in what range the random number is calculated. The range always begins with 0 and ends with "parameter-1". So if you call "Random(5)", you get a random number between 0..4. Now that's not what you want, you wanted a number from -2..2, so we sub 2 from 0..4, as a result you get a random number in the range from -2..2.

Regards, Madshi.

Author

Commented:
ok, thanks !

Author

Commented:
a great helper !

Explore More ContentExplore courses, solutions, and other research materials related to this topic.