-1

I know the code from the Standard Delphi programming goes like this:

randomize();
i := random(5,10); // where i is an integer.

Then the value of i will be between 5 and 10. However how would I do that in Fire Monkey. The function requires a range but I have no idea how to create the range.

Searching Google or event the documents on Embarcadero's website has been of no help either.

The function looks like this: function Random(const ARange: Integer): Integer;

Is this even possible, or am I looking the wrong places? Should I rather write a function like this:

while ((i<= 64) and (i>= 91)) do
    i := Random(90);
Ken White
  • 123,280
  • 14
  • 225
  • 444
Jacques Koekemoer
  • 1,378
  • 5
  • 25
  • 50

1 Answers1

3

The RNG functions in Delphi are part of the RTL, defined in the System unit. As such the are available in FMX just as they are available in VCL. In short, FireMonkey is not really relevant.

The function that you are looking for is RandomRange from System.Math.

So, you might write:

Value := RandomRange(5, 10);

But note carefully this part of the documentation:

RandomRange returns a random integer from the range that extends between AFrom and ATo (non-inclusive).

So, the function call above can only return the following values: 5, 6, 7, 8 and 9.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • That is exactly what I was looking for. How would you word that in google ? "For future when others search for this issue" – Jacques Koekemoer Aug 20 '15 at 11:51
  • @Jacques: What's wrong with *random range Delphi* or *random range integer Delphi*? Or just the help index, using *Random* (which returns *Random Support Routines* as the third listed result, and *RandomRange* as the 15th)? – Ken White Aug 20 '15 at 12:48
  • Sorry - missed XE8 in the title. The help file search is *Help->Rad Studio Help->System Library Help*, search for *Random*, where *RandomRange* is the fourth item in the results list. – Ken White Aug 20 '15 at 12:54
  • @KenWhite I actually didn't know what to search for any more. I search `random number generation firemonkey` and `generate random number between 2 values firemonkey` etc. I thought that the function had been altered between versions, the last time I used an embarcadero product was 5 years ago, back then it was as simple as `random(a,b)` – Jacques Koekemoer Aug 20 '15 at 13:58
  • In Delphi, it was never `Random(a, b)`, as far as I know. It was either `myFloat := Random;` to get a floating point value in the range `[0.0, 1.0)`, IOW, excluding 1.0, or `myInteger := Random(a);` to get a value in the range `0..a-1`. And, as David said, it has nothing to do with FMX. It belongs to the core runtime library. – Rudy Velthuis Aug 20 '15 at 21:51