Difference between revisions of "Roll"

From APL Wiki
Jump to navigation Jump to search
Miraheze>Marshall
Miraheze>Adám Brudzewsky
Line 16: Line 16:
 
Each [[scalar]] in the argument to Roll must be a non-negative integer.
 
Each [[scalar]] in the argument to Roll must be a non-negative integer.
  
The result of Roll on each number <code>n</code> 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
 
* A real number chosen uniformly at random from between 0 and 1, if n is 0; or
* One of the elements of <code>⍳n</code> chosen uniformly at random, otherwise.
+
* One of the elements of <source lang=apl inline>⍳n</source> chosen uniformly at random, otherwise.
  
 
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 class=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>.
  
 
== External links ==
 
== External links ==

Revision as of 19:20, 29 October 2019

Template:Primitive 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 n, then Roll chooses one of the first n indices, and if it is zero, then Roll chooses a floating-point number between 0 and 1.

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 non-negative integer.

The result of Roll on each number n is either

  • A real number chosen uniformly at random from between 0 and 1, if n is 0; or
  • One of the elements of n chosen uniformly at random, otherwise.

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.

External links

Lessons

APL Cultivation

Documentation

Dyalog

APLX

J Dictionary, NuVoc