delphi basics generating random number
To generate a random number in Delphi, you can use the Random
function from the System
unit. The Random
function returns a random number between 0 and a specified value.
Here's an example of how to generate a random number between 1 and 100:
uses
System.SysUtils;
var
randomNumber: Integer;
begin
Randomize; // Initialize the random number generator
randomNumber := Random(100) + 1; // Generate a random number between 1 and 100
// Use the random number as needed
// ...
end.
In the above example, Randomize
is called to initialize the random number generator with a seed value based on the current time. This ensures that each run of the program produces a different sequence of random numbers.
The Random
function is then used to generate a random number between 1 and 100. The Random
function takes an integer parameter that specifies the upper bound of the range (exclusive). By adding 1 to the result, we ensure that the random number is inclusive of the upper bound.
You can modify the code to suit your specific needs, such as changing the range of the random number or using the generated random number in your program logic.