Deal

From APL Wiki
Revision as of 08:39, 4 June 2020 by Bubbler (talk | contribs) (Created page with "{{Built-in|Deal|?}} is a dyadic primitive function which returns a random partial permutation. The name ''Deal'' comes fr...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 k≤n, and Deal generates a random k-permutation by selecting k numbers from the first n indices without replacement. Some dialects, such as J, treat Deal as a scalar function. 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.

      ?⍨10
7 4 3 10 6 2 1 8 9 5
      v←'shuffling array'
      v[?⍨≢v]
n airuhflryfags

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

      numbers←'23456789TJQKA'
      suits←'CDSH'  ⍝ Club, Diamond, Spade, Heart
      cards←,numbers∘.,suits
      cards[5?≢cards]
┌──┬──┬──┬──┬──┐
│5S│KS│JH│6D│9C│
└──┴──┴──┴──┴──┘

Description

Both arguments of k?n must be non-negative integer scalars with k≤n. 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 ⎕IO←1, and from 0 to n-1 if ⎕IO←0.

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.

External links

Documentation

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector