Using the NumPy Random Number Generator

Once you have a NumPy array, regardless of whether you’ve generated it randomly or obtained it from a more ordered source, there may be times when you need to select elements from it randomly or reorder its structure randomly. You’ll learn how to do this next.

Selecting Array Elements Randomly

Suppose you have a NumPy array of data collected from a survey, and you wish to use a random sample of its elements for analysis. The Generator object’s .choice() method allows you to select random samples from a given array in a variety of different ways. You give this a whirl in the next few examples: