SAMPLE - Generate a list of random integers from 1 to N using sample without replacement.
Author: Eddie Shore
Date: 10/1/2013
Syntax: SAMPLE(L, N)
L = length of desired list
N = high integer
If L > N, an error occurs.
Output: random sample
Program:
EXPORT SAMPLE(L,N)
BEGIN
// length, number
LOCAL I, K, T, num;
// error cond
IF L > N THEN
1/0;
END;
// main program
L1:=MAKELIST(0,X,1,L,1);
L1(1):=RANDINT(N-1)+1;
I:= 2;
REPEAT
num:=RANDINT(N-1)+1;
T:=1;
// test for uniqueness
FOR K FROM 1 TO I DO
IF num == L1(K) THEN
T:=T + 1;
END;
END;
IF T == 1 THEN
L1(I) := num;
I := I + 1;
END;
UNTIL I == L + 1;
RETURN L1;
END;
Examples:
SAMPLE(5,9) (length of 5, n = 9) can generate:
{5, 4, 8, 2, 6}
{9, 7, 8, 1, 2}
{4, 3, 6, 5, 2}
This blog is property of Edward Shore. 2013