In Delphi, you can use the Randomize function to initialize the random number generator, the Random function to generate a random floating-point value between 0 and 1, and the RandomRange function to generate a random integer within a given range.
Here’s an example of how to use these functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
uses System.SysUtils; var R: Real; I: Integer; begin //Initialize the random number generator with the current time Randomize; //Generate a random floating-point value between 0 and 1 R := Random; ShowMessage(FloatToStr(R)); //Generate a random integer between 1 and 10 I := RandomRange(1, 10); ShowMessage(IntToStr(I)); end; |
The Randomize function seeds the random number generator with a value that is based on the current time. This ensures that the random numbers generated will be different each time the program is run.
The Random function returns a random floating-point value between 0 and 1, while the RandomRange function returns a random integer within the specified range.
You can find more information about these functions and other ways to generate random numbers in Delphi in the Delphi documentation.
Leave a Reply