Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random number in a float range

How we can generate randomize number between a range in the Float numbers (in delphi xe3) ?

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

[ 0.20 , 0.32 , 0.10 , 0.50 ]

Thanks for solutions....

like image 212
User Avatar asked Dec 06 '12 22:12

User


2 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 a single call to Randomizeor set the RandSeed parameter before calling the Random function for the first time.

Not doing so, generates the same sequence every time you run the program. Note however, that this sequence is not guaranteed when recompiling for another compiler version.

like image 39
LU RD Avatar answered Sep 28 '22 01:09

LU RD


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

RandomRange(10, 90 + 1) / 100

or

RandomRange(10, 90 + 1) * 0.01

will return numbers in the range of 0.10 to 0.90 (including 0.90)

like image 148
kobik Avatar answered Sep 28 '22 00:09

kobik