Difference between revisions of "Samples and permutations"

From Lazarus wiki
Jump to navigationJump to search
(Creating "See also" section)
(Improving grammar.)
Line 1: Line 1:
Functions for samples and permutations randomly ordered vectors of data. Random samples may be used for very diverse applications, e.g. simulating rolls of a die, generating anonymised IDs (e.g. for clinical trials), Monte Carlo simulations and wide-range statistical applications.
+
Functions for samples and permutations deliver randomly ordered vectors of data. Random samples may be used for very diverse applications, e.g. simulating rolls of a die, generating anonymised IDs (e.g. for clinical trials), Monte Carlo simulations and wide-range statistical applications.
  
 
The following function delivers a vector of longint values, which are randomly ordered and range from min to max:
 
The following function delivers a vector of longint values, which are randomly ordered and range from min to max:

Revision as of 23:12, 19 November 2020

Functions for samples and permutations deliver randomly ordered vectors of data. Random samples may be used for very diverse applications, e.g. simulating rolls of a die, generating anonymised IDs (e.g. for clinical trials), Monte Carlo simulations and wide-range statistical applications.

The following function delivers a vector of longint values, which are randomly ordered and range from min to max:

type
  TLongintvector_data = array of longint;   

function sample(min, max: longint; replace: boolean): TLongintvector_data;
var
  i, j, num1, num2: longint;
  Vector1, Vector2: TLongintvector_data;
begin
  randomize;                        // initialise random number generator
  num1 := 1 + max - min;            // length of vector to be generated
  SetLength(Vector1, num1);         // vector for original (ordered) data
  SetLength(Vector2, num1);         // vector with permuted data
  if num1 > 0 then
  begin
    for i := 0 to num1 - 1 do
    begin                           
      Vector1[i] := min + i;        // initialise original vector
    end;
    num2 := num1;
    for i := 1 to num1 do
    begin
      j := random(num2);            // select random element of Vector1
      Vector2[i - 1] := Vector1[j]; // and assign to next element of Vector2
      if not replace then
      begin
        Delete(Vector1, j, 1);      // remove randomly selected element
        Dec(num2);                  // and adapt size
      end;
    end;
  end;
  result := Vector2;
end;

Depending on the value of the parameter replace, the random elements of the vector are provided with or without replacement.

See also