pf_random_subset Interface

public interface pf_random_subset

Fills idx with the first size(idx) elements of the permutation of 1 .. m under seed.

A uniform random subset of size size(idx) drawn without replacement, in uniform random order -- and it costs O(size(idx)) rather than O(m), which is the reason this permutation is coordinate-addressed at all: 1000 rows out of 10**12 reads 1000 elements and never materialises the population. pf_random_subset(idx, size(idx), seed) is pf_random_permutation(idx, seed), and a subset of size n is a prefix of one of size 2n.

idx is a rank-1 integer(int32) or integer(int64) array, intent(out); m is integer(int32) or integer(int64); seed is integer(int64). A zero-sized idx is a defined no-op and is not validated -- it asks for nothing, so no precondition applies to it. threads behaves exactly as it does on pf_random_permutation, including the work floor; see there. Note the floor is measured in elements PRODUCED, size(idx), not in m -- a 100-element subset of a population of 10**12 is 100 elements of work.

Three preconditions, all of which abort rather than truncate or wrap when idx is non-empty: m >= 1; size(idx) <= m, since a subset drawn without replacement cannot be larger than its population; and -- for an integer(int32) idx only -- m <= huge(int32), since an element may be any value in [1, m] and one above huge(int32) has nowhere to go. The last of those is why the integer(int32)-array/integer(int64)-m pairing is accepted at compile time and rejected at run time: refusing it in the interface would make a perfectly ordinary integer :: m fail to compile against an integer(int64) array.


Module Procedures

private subroutine pf_random_subset_i32_i32(idx, m, seed, threads)

pf_random_subset for an integer(int32) result and an integer(int32) population size.

Arguments

Type IntentOptional Attributes Name
integer(kind=int32), intent(out) :: idx(:)

filled with the first size(idx) elements

integer(kind=int32), intent(in) :: m

population size; the permutation is of 1 .. m

integer(kind=int64), intent(in) :: seed

the permutation family's seed

integer, intent(in), optional :: threads

thread request; absent means automatic

private subroutine pf_random_subset_i32_i64(idx, m, seed, threads)

pf_random_subset for an integer(int32) result and an integer(int64) population size.

Arguments

Type IntentOptional Attributes Name
integer(kind=int32), intent(out) :: idx(:)

filled with the first size(idx) elements

integer(kind=int64), intent(in) :: m

population size; must not exceed huge(int32)

integer(kind=int64), intent(in) :: seed

the permutation family's seed

integer, intent(in), optional :: threads

thread request; absent means automatic

private subroutine pf_random_subset_i64_i32(idx, m, seed, threads)

pf_random_subset for an integer(int64) result and an integer(int32) population size.

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(out) :: idx(:)

filled with the first size(idx) elements

integer(kind=int32), intent(in) :: m

population size; the permutation is of 1 .. m

integer(kind=int64), intent(in) :: seed

the permutation family's seed

integer, intent(in), optional :: threads

thread request; absent means automatic

private subroutine pf_random_subset_i64_i64(idx, m, seed, threads)

pf_random_subset for an integer(int64) result and an integer(int64) population size.

Arguments

Type IntentOptional Attributes Name
integer(kind=int64), intent(out) :: idx(:)

filled with the first size(idx) elements

integer(kind=int64), intent(in) :: m

population size; the permutation is of 1 .. m

integer(kind=int64), intent(in) :: seed

the permutation family's seed

integer, intent(in), optional :: threads

thread request; absent means automatic