Generate a random number in a float range

How can we generate a random number between a range in float numbers (in delphi xe3)?

For example, randomize a number between [0.10 to 0.90] . I need to give results, for example:

[ 0.20 , 0.32 , 0.10 , 0.50 ]

Thanks for the decision ....

+4
source share
3 answers
 var float : Double; float := Random; // Random float in range: 0 <= float < 1 float := 0.1 + float*0.8 // 0.1 <= float < 0.9 

To initialize the random number generator, make one call to Randomize or set the RandSeed parameter before calling the Random function for the first time.

Do not do this, generates the same sequence each time the program starts. Note, however, that this sequence is not guaranteed when recompiling for another version of the compiler.

+4
source

Another option is to use RandomRange (returns: AFrom <= r < ATo ) as follows:

 RandomRange(10, 90 + 1) / 100 

or

 RandomRange(10, 90 + 1) * 0.01 

will return numbers between 0.10 and 0.90 (including 0.90)

+5
source
 function RandomRangeF(min,max:single):single; var float : single; begin float := Random; result := min + float* (max-min); end; 
0
source

All Articles