Making random numbers in Delphi
It's easy. In summary:
- Start your program by calling the randomize() function. This resets the random number generator built into Delphi and makes sure that you don't always get the same number. You need only do this once at the beginning.
- Use the random() function without parameters to get a random number between 0 and 1
- Use the random() function with an integer parameter to get an integer number between 0 and the number that you specify (minus one :-( )
uses
System.SysUtils;
var
singlerandom : single;
integerrandom : integer;
begin
// use randomize() once at the beginning of a program
// othewise you'll always get the same numbers
randomize();
// Example, generate a random number between 0 and 1
singlerandom := random();
// Example, generate a random number between 1 and 100
integerrandom := random(100)+1;
// Random(100) generates a number between 0 and 99
writeln(singlerandom);
writeln(integerrandom);
readln;
end.