From APL Wiki
Jump to navigation Jump to search

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 k?n must be non-negative integer scalars with kn, and Deal generates a random k-permutation by selecting k numbers from the first n indices without replacement. Deal shares the glyph ? with the monadic scalar function Roll.


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

7 4 3 10 6 2 1 8 9 5
      v'shuffling array'
n airuhflryfags

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

      suits'CDSH'  ⍝ Club, Diamond, Spade, Heart

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 ?0, which uses the rank operator .

      ?~ 5$10  NB. Generate five permutations of 0 to 9 at once
9 8 3 0 6 1 2 5 4 7
7 2 4 1 0 5 6 9 8 3
3 0 7 2 9 1 5 8 4 6
2 1 5 0 3 6 4 7 8 9
3 7 5 9 0 2 6 8 4 1
Works in: J


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

The possible number of permutations generated for particular values of k and n is .

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

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 ⎕RL. 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.

External links


APL built-ins [edit]
Primitive functions
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-beamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Case convertUnicode convert
Operators SearchReplace