How do I generate a random number between the values given by the user?
The Random
function has two overloads. The variant with no parameters returns real values between 0 and 1. The other variant accepts an integer parameter N
and returns integers i
such that 0 <= i < N
. So you can use some arithmetic to produce values in the range a
to b
.
function RandInRange(const a, b: Integer): Integer;
begin
Result := a + Random(b-a+1);
end;
This returns integers i
that satisfy a <= i <= b
and that are uniformly distributed. Well, the distribution is only as good as the underlying PRNG used by Random
.
Note that I've made no attempt to check or enforce that a <= b
.
Alternatively, as is pointed out in the comments, you could use RandomRange
from the Maths
unit. Although to beware that it has a different convention and returns values in the range a <= i < b
.
Looking at the rest of your code, you should not use a var
parameter to pass the string grid reference. You are not going to change the reference. You might call methods on the string grid, or access properties, but you are not going to modify the reference.
And as for that string grid, you do not do anything with it. You populate an array which you immediately throw away. You will need to assign to the elements of the string grid. The arrays are pointless and can be removed.