phit

Sampling functions

Explicitly seeded samplers. Every call starts its own stream from the seed. See Seeded sampling.

Uniform, categorical, and permutations

dist_uniform_rng()

Seeded uniform samples on [min, max], like R’s runif(): min + (max − min) · u.

  • min equal to max returns min.
  • Each call starts its own ChaCha20 stream from seed, one uniform per draw. The same seed and arguments reproduce the same draws, and a longer request extends a shorter one.
Phit\dist_uniform_rng(    int $n,    int $seed,    float $min = 0.0,    float $max = 1.0,): array
Parameter Type Default Description
$n int required Sample count in [0, 1000000]
$seed int required Nonnegative PHP integer; every call restarts its own stream
$min float 0.0 Lower bound
$max float 1.0 Upper bound, ≥ min

Returns list<float>

Errors Exception: code 1001 for invalid input, 1003 for nonfinite input, or 1007 for more than 1,000,000 draws.

See also Details · Example

dist_categorical_rng()

Seeded 0-based category indices drawn with probabilities prob, like R’s sample(x, n, replace = TRUE, prob = prob).

  • Weights are divided by their sum. Each draw is the first k with u · sum(prob) < prob[0] + … + prob[k].
  • Each call starts its own ChaCha20 stream from seed, one uniform per draw. The same seed and arguments reproduce the same draws, and a longer request extends a shorter one.
Phit\dist_categorical_rng(    int $n,    int $seed,    array $prob,): array
Parameter Type Default Description
$n int required Sample count in [0, 1000000]
$seed int required Nonnegative PHP integer; every call restarts its own stream
$prob array required Finite, nonnegative weights with a positive sum

Returns list<int>

Errors Exception: code 1001 for invalid input, 1003 for nonfinite input, or 1007 for more than 1,000,000 draws.

See also Details

dist_categorical_rows_rng()

One seeded 0-based category index per row of prob, drawn with that row’s probabilities, as for ordered-logit outcomes.

  • Draw i uses the i-th uniform, so equal rows give the result of dist_categorical_rng().
  • Each call starts its own ChaCha20 stream from seed, one uniform per draw. The same seed and arguments reproduce the same draws, and a longer request extends a shorter one.
Phit\dist_categorical_rows_rng(    int $seed,    array $prob,): array
Parameter Type Default Description
$seed int required Nonnegative PHP integer; every call restarts its own stream
$prob array required At most 1000000 rows of finite, nonnegative weights with a positive sum

Returns list<int>

Errors Exception: code 1001 for invalid input, 1003 for nonfinite input, or 1007 for more than 1,000,000 draws.

See also Details

permutation_rng()

Seeded sample of size distinct 0-based indices from 0 to n − 1 in random order, like R’s sample(n, size) minus one.

  • size: null gives a permutation of all n.
  • Uses a partial Fisher–Yates shuffle: step i swaps position i with i + floor(u · (n − i)).
  • Each call starts its own ChaCha20 stream from seed, one uniform per draw. The same seed and arguments reproduce the same draws, and a longer request extends a shorter one.
Phit\permutation_rng(    int $n,    int $seed,    ?int $size = NULL,): array
Parameter Type Default Description
$n int required Population size in [0, 1000000]
$seed int required Nonnegative PHP integer; every call restarts its own stream
$size ?int NULL Draws in [0, n]; null gives a permutation

Returns list<int>

Errors Exception: code 1001 for invalid input, 1003 for nonfinite input, or 1007 for more than 1,000,000 draws.

See also Details