routine roll
Documentation for routine roll
assembled from the following types:
role Baggy
From Baggy
(Baggy) method roll
Defined as:
multi method roll(Baggy: --> Any)multi method roll(Baggy: --> Seq)
Like an ordinary list roll, but returns keys of the invocant weighted by their values, as if the keys were replicated the number of times indicated by the corresponding value and then list roll used. The underlying metaphor for rolling is that you're throwing $count
dice that are independent of each other, which (in bag terms) is equivalent to picking a colored marble out your bag and then putting it back, and doing this $count
times. In dice terms, the number of marbles corresponds to the number of sides, and the number of marbles of the same color corresponds to the number of sides with the same color. (For "picking without replacement" see pick instead).
If *
is passed to $count
, returns a lazy, infinite sequence of randomly chosen elements from the invocant.
my = bag <eggs bacon bacon bacon>;say .roll; # OUTPUT: «bacon»say .roll(3); # OUTPUT: «(bacon eggs bacon)»my := .roll(*);say [^5]; # OUTPUT: «(bacon eggs bacon bacon bacon)»
role Setty
From Setty
(Setty) method roll
multi method roll( = 1)
Returns a lazy list of $count
elements, each randomly selected from the set. Each random choice is made independently, like a separate die roll where each die face is a set element.
If *
is passed as $count
, the list is infinite.
class Range
From Range
(Range) method roll
multi method roll(Range: --> Any)multi method roll(Range: --> Seq)
Performs the same function as Range.list.roll
, but attempts to optimize by not actually generating the list if it is not necessary.
enum Bool
From Bool
(Bool) routine roll
multi method roll(Bool --> Bool)multi method roll(Bool --> Seq)
Returns True
or False
if called without any argument. Otherwise returns $count
elements chosen at random. Note that each random choice from the enum
is made independently, like a separate coin toss where each side of the coin represents one of the two values of the enum
. If *
is passed as $count
an infinite Seq of Bool
s is returned.
say Bool.roll; # OUTPUT: «True»say Bool.roll(3); # OUTPUT: «(True False False)»say Bool.roll(*); # OUTPUT: «(...)»
role Enumeration
From Enumeration
(Enumeration) method roll
Defined as:
multi method roll(::?CLASS:)multi method roll(::?CLASS: \n)multi method roll(::?CLASS: *)
They work on the defined class selecting one or n
elements without eliminating them.
say Norse-gods.roll() for ^3; # OUTPUT: «FreijaFreijaOðin»
role Mixy
From Mixy
(Mixy) method roll
method roll( = 1)
Similar to a Bag.roll, but with Real
weights rather than integral ones.
class Any
From Any
(Any) method roll
Defined as:
multi method roll(--> Any)multi method roll( --> Seq)
Coerces the invocant to a list
by applying its .list
method and uses List.roll
on it.
my Mix = ("þ" xx 3, "ð" xx 4, "ß" xx 5).Mix;say .roll; # OUTPUT: «ð»say .roll(5); # OUTPUT: «(ß ß þ ß þ)»
$m
, in this case, is converted into a list and then a (weighted in this case) dice is rolled on it. See also List.roll
for more information.
class List
From List
(List) routine roll
Defined as:
multi sub roll(, * --> Seq)multi method roll(List: --> Seq)multi method roll(List: --> Mu)
If $count
is supplied: Returns a sequence of $count
elements, each randomly selected from the list. Each random choice is made independently, like a separate die roll where each die face is a list element. If *
is passed as $count
returns a lazy, infinite sequence of randomly chosen elements from the original list.
If $count
is omitted: Returns a single random item from the list, or Nil if the list is empty
Examples:
say <a b c d e>.roll; # 1 random lettersay <a b c d e>.roll: 3; # 3 random letterssay roll 8, <a b c d e>; # 8 random lettersmy := (^10).roll(*);say [^15]; # 15 random digits