Difference between revisions of "Roll"

From APL Wiki
Jump to navigation Jump to search
Miraheze>Adám Brudzewsky
m
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com")
Tags: Mobile web edit, Mobile edit
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Primitive|?|Roll}} is a [[monadic]] [[scalar function]] which returns random or pseudo-random numbers. Each [[scalar]] in the argument must be a non-negative integer. If it is a positive number <source lang=apl inline>n</source>, then Roll chooses one of the first <source lang=apl inline>n</source> [[Index|indices]], and if it is zero, then Roll chooses a floating-point number between 0 and 1.
+
{{Built-in|Roll|?}} is a [[monadic]] [[scalar function]] which returns random or pseudo-random numbers. The name ''Roll'' comes from the analogy of rolling dice. Each [[scalar]] in the argument must be a positive integer, <source lang=apl inline>n</source>, and Roll chooses one of the first <source lang=apl inline>n</source> [[Index|indices]]. Some dialects allow <source lang=apl inline>n</source> to be zero, in which case Roll chooses a floating-point number between 0 and 1. Roll shares its [[glyph]] with the other random-result function, [[Deal]].
  
 
== Examples ==
 
== Examples ==
Line 11: Line 11:
 
eeHHHolo
 
eeHHHolo
 
</source>
 
</source>
 +
{{Works in|[[Dyalog APL]], [[NARS2000]], [[dzaima/APL]]}}
  
 
== Description ==
 
== Description ==
  
Each [[scalar]] in the argument to Roll must be a non-negative integer.
+
Each [[scalar]] in the argument to Roll must be a positive integer.
  
 
The result of Roll on each number <source lang=apl inline>n</source> is either
 
The result of Roll on each number <source lang=apl inline>n</source> is either
* A real number chosen uniformly at random from between 0 and 1, if n is 0; or
+
* One of the elements of <source lang=apl inline>⍳n</source> chosen uniformly at random; or
* One of the elements of <source lang=apl inline>⍳n</source> chosen uniformly at random, otherwise.
+
* A real number between 0 and 1 chosen uniformly at random, if <source lang=apl inline>n</source> is 0 and the dialect has this extension.
  
 
Because [[Iota]] depends on [[index origin]], Roll depends on index origin unless every number in the argument is 0.
 
Because [[Iota]] depends on [[index origin]], Roll depends on index origin unless every number in the argument is 0.
  
The choices made by Roll 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]] <source lang=apl inline>⎕RL</source>.
+
The choices made by Roll 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]] <source lang=apl inline>⎕RL</source>. Traditionally, APL uses the [[wikipedia:Lehmer random number generator|Lehmer random number generator]], but [[Dyalog APL]] defaults to the allow choosing a different random number generator and defaults to the [[wikipedia:Mersenne Twister|Mersenne Twister]].
  
 
== External links ==
 
== External links ==
Line 28: Line 29:
 
=== Lessons ===
 
=== Lessons ===
  
[https://chat.stackexchange.com/transcript/52405?m=41303127#41303127 APL Cultivation]
+
* [https://chat.stackexchange.com/transcript/52405?m=41303127#41303127 APL Cultivation]
 +
 
 +
* [https://nbviewer.jupyter.org/github/Dyalog/dyalog-jupyter-notebooks/blob/master/Random%20Numbers.ipynb Jupyter Notebook] (Run on [https://tryapl.org/?notebook=https://github.com/Dyalog/dyalog-jupyter-notebooks/blob/master/Random%20Numbers.ipynb TryAPL])
  
 
=== Documentation ===
 
=== Documentation ===
  
[http://help.dyalog.com/latest/Content/Language/Primitive%20Functions/Roll.htm Dyalog]
+
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Roll.htm Dyalog]
  
[http://microapl.com/apl_help/ch_020_020_170.htm APLX]
+
* [http://microapl.com/apl_help/ch_020_020_170.htm APLX]
  
J [https://www.jsoftware.com/help/dictionary/d640.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/query NuVoc]
+
* [https://www.jsoftware.com/help/dictionary/d640.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/query J NuVoc]
 +
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]]

Latest revision as of 14:28, 14 July 2020

?

Roll (?) is a monadic scalar function which returns random or pseudo-random numbers. The name Roll comes from the analogy of rolling dice. Each scalar in the argument must be a positive integer, n, and Roll chooses one of the first n indices. Some dialects allow n to be zero, in which case Roll chooses a floating-point number between 0 and 1. Roll shares its glyph with the other random-result function, Deal.

Examples

      ? 4  6                   ⍝ Roll four six-sided dice
1 4 1 1
      ? 3 2 1 0                 ⍝ The result for 1 is always 1
2 2 1 0.9637543707
      {[?8⍴≢]} 'Hello'        ⍝ Choose 8 random letters
eeHHHolo

Description

Each scalar in the argument to Roll must be a positive integer.

The result of Roll on each number n is either

  • One of the elements of n chosen uniformly at random; or
  • A real number between 0 and 1 chosen uniformly at random, if n is 0 and the dialect has this extension.

Because Iota depends on index origin, Roll depends on index origin unless every number in the argument is 0.

The choices made by Roll 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. Traditionally, APL uses the Lehmer random number generator, but Dyalog APL defaults to the allow choosing a different random number generator and defaults to the Mersenne Twister.

External links

Lessons

Documentation

APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
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 Name classCase convertUnicode convert
Operators SearchReplace