# Pick random element

In a nutshell, picking an element from a list is implemented with a method conveniently called "pick":

```perl
say (1, 2, 3).pick;
```

There are various ways of doing something similar, though.\
Raku has actually two methods (with associated functional forms) to return random elements depending on whether you are doing selection with or without replacement.

Selection with replacement: (roll of a die)

```perl
say (1..6).roll;          # return 1 random value in the range 1 through 6
say (1..6).roll(3);       # return a list of 3 random values in the range 1 through 6
say (1..6).roll(*)[^100]; # return first 100 values from a lazy infinite list of random values in the range 1 through 6
```

Selection without replacement: (pick a card from a deck)

```perl
# define the deck
my @deck = <2 3 4 5 6 7 8 9 J Q K A> X~ <♠ ♣ ♥ ♦>;
say @deck.pick;    # Pick a card
say @deck.pick(5); # Draw 5
say @deck.pick(*); # Get a shuffled deck
```

Or you can always use the normal `rand` built-in to generate a subscript (which automatically truncates any fractional part):

```perl
@array[@array * rand]
```

However, the `pick` and `roll` methods (not to be confused with the pick-and-roll method in basketball) are more general insofar as they may be used on any enumerable type:

```perl
say Bool.pick;  # returns either True or False
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/perl6-rosettacode/programming_tasks/p/pick_random_element.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
