Sign in Join
Wiki copytemporary

Samples and permutations: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Samples and permutations

OldNewLine
1+Functions for samples and permutations deliver randomly ordered vectors of data. Random samples may be used for very diverse applications, including simulating rolls of a die, generating anonymised IDs (e.g. for clinical trials), Monte Carlo simulations and wide-range statistical applications.
2+
3+The following function delivers a vector of **longint** values, which are randomly ordered and range from **min** to **max**:
4+
5+``` pascal
6+type
7+ TLongintvector_data = array of longint;
8+
9+function sample(min, max: longint; replace: boolean): TLongintvector_data;
10+var
11+ i, j, num1, num2: longint;
12+ Vector1, Vector2: TLongintvector_data;
13+begin
14+ randomize; // initialise random number generator
15+ num1 := 1 + max - min; // length of vector to be generated
16+ SetLength(Vector1, num1); // vector for original (ordered) data
17+ SetLength(Vector2, num1); // vector with permuted data
18+ if num1 > 0 then
19+ begin
20+ for i := 0 to num1 - 1 do
21+ begin
22+ Vector1[i] := min + i; // initialise original vector
23+ end;
24+ num2 := num1;
25+ for i := 1 to num1 do
26+ begin
27+ j := random(num2); // select random element of Vector1
28+ Vector2[i - 1] := Vector1[j]; // and assign to next element of Vector2
29+ if not replace then
30+ begin
31+ Delete(Vector1, j, 1); // remove randomly selected element
32+ Dec(num2); // and adapt size
33+ end;
34+ end;
35+ end;
36+ result := Vector2;
37+end;
38+```
39+
40+Depending on the value of the parameter **replace**, the random elements of the vector are provided with or without replacement. With replacement, one number may occur several times. Without replacement all elements of the vector will contain unique numbers.
41+
42+## See also
43+
44+- [[Ranking vectors of data]]
45+- [[Dev random]]
46+- [[Functions for descriptive statistics]]
47+- [[Generating Random Numbers]]
48+- [[Marsaglia&|Marsaglia's pseudo random number generators]]
49+- [[A simple implementation of the Mersenne twister]]
50+- [[Delphi compatible LCG Random]]
51+- [[EverettRandom]]
52+
53+---
54+*From the Free Pascal wiki, page "Samples and permutations", as it stood on 2021-03-06 after a change by Jwdietrich, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
55+Category: Code
56+Category: Mathematics
57+Category: Modelling and Simulation
58+Category: Statistical algorithms

Back to history