search
Search
Unlock 100+ guides
search toc
close
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# NumPy | choice method

schedule Aug 10, 2023
Last updated
local_offer
PythonNumPy
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

NumPy's `choice()` method returns an array of random samples.

# Parameters

1. `a`link | `int` or 1D array-like

• If an `int` is given, then random integer is generated between 0 (inclusive) and `int` (exclusive).

• If array-like is given, then elements are randomly selected from the array-like.

2. `size`link | `int` or tuple of `int`s | `optional`

• If an `int` is given, then `size` represents number of random numbers to generate.

• If a tuple is given, then `size` represents the output shape.

By default, a single value is returned.

3. `replace`link | `boolean` | `optional`

• If `True`, then the method will sample with replacement, that is, an element can be chosen multiple times.

• If `False`, then the method will sample without replacement, that is, an element can be only chosen once.

By default, `replace=True`.

4. `p` | 1D array-like of numbers | `optional`

The probabilities that will be used to generate `a`. By default, a uniform distribution will be used.

# Return Value

If `size` is supplied, then a NumPy array is returned.

# Examples

## Generating a random integer between 0 (inclusive) and 5 (exclusive)

To generate a single number between 0 (inclusive) and 5 (exclusive):

``` np.random.choice(5) 3 ```

## Generating multiple random integers between 0 (inclusive) and 5 (exclusive)

To generate multiple numbers between 0 (inclusive) and 5 (exclusive):

``` np.random.choice(5, size=3) array([4, 4, 2]) ```

## Generating multiple random integers with replacement

To generate multiple numbers with replacement:

``` np.random.choice(5, size=3) # replace=True array([2, 4, 2]) ```

Notice how the value 2 was chosen twice.

## Generating multiple random integers without replacement

To generate multiple numbers without replacement:

``` np.random.choice(5, size=3, replace=False) array([4, 2, 1]) ```

Here, the randomly selected values are guaranteed to be unique.

## Randomly selecting values from an array

To randomly select two values from a given array:

``` np.random.choice([2,4,6,8], size=2) array([4, 2]) ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!