routine roll

Documentation for routine roll assembled from the following types:

role Baggy

From Baggy

(Baggy) method roll

Defined as:

multi method roll(Baggy:D: --> Any:D)
multi method roll(Baggy:D: $count --> Seq:D)

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 $breakfast = bag <eggs bacon bacon bacon>;
say $breakfast.roll;                                  # OUTPUT: «bacon␤» 
say $breakfast.roll(3);                               # OUTPUT: «(bacon eggs bacon)␤» 
 
my $random_dishes := $breakfast.roll(*);
say $random_dishes[^5];                               # OUTPUT: «(bacon eggs bacon bacon bacon)␤»

role Setty

From Setty

(Setty) method roll

multi method roll($count = 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:D:         --> Any:D)
multi method roll(Range:D: $number --> Seq:D)

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:U --> Bool:D)
multi method roll(Bool:U $count --> Seq:D)

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 Bools 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:U:)
multi method roll(::?CLASS:U: \n)
multi method roll(::?CLASS:D: *@pos)

They work on the defined class selecting one or n elements without eliminating them.

say Norse-gods.roll() for ^3;  # OUTPUT: «Freija␤Freija␤Oðin␤» 

role Mixy

From Mixy

(Mixy) method roll

method roll($count = 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($n --> Seq)

Coerces the invocant to a list by applying its .list method and uses List.roll on it.

my Mix $m = ("þ" xx 3"ð" xx 4"ß" xx 5).Mix;
say $m.roll;    # OUTPUT: «ð␤» 
say $m.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($count*@list --> Seq:D)
multi method roll(List:D: $count --> Seq:D)
multi method roll(List:D: --> 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 letter 
say <a b c d e>.roll: 3;    # 3 random letters 
say roll 8, <a b c d e>;    # 8 random letters 
 
my $random-digits := (^10).roll(*);
say $random-digits[^15];    # 15 random digits