Functions Delphi

function RandomRange ( const RangeFrom, RangeTo : Integer ) : Integer;


Description
The RandomRange function generates a random Integer number within the range RangeFrom to RangeTo inclusively. *

This provides a more convenient version of the System unit Random function.

Both use a pseudo random number sequence of 232 values. Each time you run your program, the values generated will be the same, unless you reposition the generator to a different part of the sequence using the Randomize or RandSeed functions.

Notes
* Warning
The RandomRange function does not appear to work as documented by Borland. It never includes the RangeTo value in the result. So it appears to be only partially inclusive.
(Thanks to Jerry Keyes for pointing this out).


Related commands
Random Generate a random floating point or integer number
Randomize Reposition the Random number generator next value
RandSeed Reposition the Random number generator next value

Example code : Generate random numbers in a very small range
var
i : Integer;
begin
// Show 5 random numbers in the range 652 to 656
for i := 1 to 5 do
ShowMessage('Random number : '+IntToStr(RandomRange(652, 656)));
end;

Show full unit code
Random number = 652
Random number = 652
Random number = 655
Random number = 652
Random number = 653