Roll: Difference between revisions
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com") Tags: Mobile edit Mobile web edit |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
Line 1: | Line 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, < | {{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, <syntaxhighlight lang=apl inline>n</source>, and Roll chooses one of the first <syntaxhighlight lang=apl inline>n</source> [[Index|indices]]. Some dialects allow <syntaxhighlight 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 == | ||
< | <syntaxhighlight lang=apl> | ||
? 4 ⍴ 6 ⍝ Roll four six-sided dice | ? 4 ⍴ 6 ⍝ Roll four six-sided dice | ||
1 4 1 1 | 1 4 1 1 | ||
Line 17: | Line 17: | ||
Each [[scalar]] in the argument to Roll must be a positive integer. | Each [[scalar]] in the argument to Roll must be a positive integer. | ||
The result of Roll on each number < | The result of Roll on each number <syntaxhighlight lang=apl inline>n</source> is either | ||
* One of the elements of < | * One of the elements of <syntaxhighlight lang=apl inline>⍳n</source> chosen uniformly at random; or | ||
* A real number between 0 and 1 chosen uniformly at random, if < | * A real number between 0 and 1 chosen uniformly at random, if <syntaxhighlight 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]] < | 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]] <syntaxhighlight 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 == |
Revision as of 21:21, 10 September 2022
?
|
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, <syntaxhighlight lang=apl inline>n</source>, and Roll chooses one of the first <syntaxhighlight lang=apl inline>n</source> indices. Some dialects allow <syntaxhighlight 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
<syntaxhighlight lang=apl>
? 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 </source>
Description
Each scalar in the argument to Roll must be a positive integer.
The result of Roll on each number <syntaxhighlight lang=apl inline>n</source> is either
- One of the elements of <syntaxhighlight lang=apl inline>⍳n</source> chosen uniformly at random; or
- A real number between 0 and 1 chosen uniformly at random, if <syntaxhighlight 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.
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 <syntaxhighlight lang=apl inline>⎕RL</source>. 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
- Jupyter Notebook (Run on TryAPL)