Skip to main content

NumPy Selective Random

In this tutorial you will learn:

  • What is selective random?
  • NumPy choice() function
  • How to use choice() function

Selective Random

Selective random is a technique for random sampling in which the data set from which the sample has to be selected is dependent on the specific pre-defined values. For example if we want to randomly select the professor of certain university who can proficiently carry out a specific research, here we would provide specific data set of those professors (which meet the criteria) to the random function, which will then choose the professor(s) randomly.

Choice() Function

To implement random sampling on a given data set, the NumPy provides a function “choice()” which takes in a data set in the form of an array as a parameter, randomly select one of the element from the data set and returns it. The function was added in NumPy 1.7.0. The choice() function can take in 4 parameters, out of which one is mandatory and three are optional. The first parameter is a 1D array from which we want to choose the random element, second argument is the size of output array, third argument determines whether the sample being returned by the function will be with replacement or without replacement. The fourth argument, is a 1D array which is of the exact size of the first parameter and it assigns the probability of selection to each element.
In this example we will select a number randomly from the given data set of [2,4,6,8].

  1. print(' ** NumPy Selective Random **')
  2. from numpy import random as r
  3. #defining input
  4. a=[2,4,6,8]
  5. print('1D input array:',a)
  6. #using the choice() function
  7. res = r.choice(a)
  8. #printing result
  9. print('The selected number is', res)

In this example we will be randomly selecting a character from a 1D array containing characters from ‘a’ to ‘e’
  1. from numpy import random as r
  2. #defining input
  3. a=['a','b','c','d','e']
  4. print('1D input array:',a)
  5. #using the choice() function
  6. res = r.choice(a)
  7. #printing result
  8. print('The selected number is', res)

In this example we will get a 2D array(2,4) as output from the choice() function, here you can see that the 1D input array has only 7 numbers while the output array requires 8 elements, so in result you can observe that multiple times the numbers in output array will be repeated.
  1. from numpy import random as r
  2. #defining input
  3. a=[2,4,6,8]
  4. print('1D input array:',a)
  5. #using choice() function along with the output array size as (2,4)
  6. res = r.choice(a,size=(2,4))
  7. #printing result
  8. print('The 2D output array is:', res)


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.