Deal

is a dyadic primitive function which returns a random partial permutation. The name Deal comes from the analogy of dealing cards in a card game such as Poker. Both arguments of  must be non-negative integer scalars with , and Deal generates a random k-permutation by selecting   numbers from the first   indices without replacement. Deal shares the glyph  with the monadic scalar function Roll.

Examples
Deal can be used to generate a random permutation of a given size, and shuffle an array using it.

It can also simulate a card game, e.g. deal a hand of 5 cards out of a standard 52-card deck.

J assigns rank 0 to Deal, so it can be used to generate multiple permutations at once. This behavior can be mimicked in other APLs by writing, which uses the rank operator.

Description
Both arguments of  must be non-negative integer scalars with. The result of Deal is a vector of length, whose elements are pairwise distinct random values picked from.

The possible number of permutations generated for particular values of  and   is $$P(n,k) = \frac{n!}{(n-k)!}$$.

Deal depends on index origin. In particular, it picks k numbers from 1 to n if, and from 0 to n-1 if.

The choices made by Deal do not have to be truly random: they may be pseudorandom (generated by a deterministic but difficult to predict algorithm) or taken from the operating system. They way random numbers are generated is controlled by the random link. Note that using pseudorandom numbers for Deal may not be ideal for generating large permutations, since the possible number of permutations generated is bounded by the cycle length of the random number generator used. Traditionally, APL uses the Lehmer random number generator, but Dyalog APL defaults to the Mersenne Twister.

Documentation

 * Dyalog


 * APLX


 * J Dictionary, J NuVoc