[fpc-devel] Array of random integers

Bart bartjunk64 at gmail.com
Sun Apr 16 13:21:47 CEST 2017


Would a function that returns an array of unique (=non-repeating)
random integers in a certain range be accepted as a contribution to
e.g. the math unit?

I had the following function prototypes in mind:

  Returns an array of aSize random integers without duplicates ranging from
  aFrom (inclusive) to aTo (exclusive).
  - aFrom and aTo must be positive integers
  - aTo must be > aFrom
  - aSize must be <= aTo - aFrom
  - if either of these conditions is not met, an empty array will be returned.
function RandomArray(aFrom, aTo, aSize: Integer): TIntegerDynArray; overload;
function RandomArray(aFrom, aTo: Integer): TIntegerDynArray; overload;
function RandomArray(Range: Integer): TIntegerDynArray; overload;

function RandomArray(aFrom, aTo, aSize: Int64): TInt64DynArray; overload;
function RandomArray(aFrom, aTo: Int64): TInt64DynArray; overload;
function RandomArray(Range: Int64): TInt64DynArray; overload;

( TInt64DynArray = array of Int64)

My current implementation builds such an array of size 1 million
32-bit integers in 59 msec (on a 7 year old laptop with an i5 CPU M
430 @ 2.27 GHz with 4 GB Ram).


More information about the fpc-devel mailing list