No votes yet

Random choice generator

A random choice generator picks one option from a list you named. In Python, numpy.random.choice does the same job on an array. Name the options first.

Time By Online Alarm Clock

How it works

You supply the options. The tool returns one of them, or several if you asked for a size. It will not invent a fifth restaurant when you named four. It will not know which option is kind. A page widget and a programming call are the same idea: an array, a draw, a result that already lived in the array. If the screen prints something that was not in the list, you are not on this tool.

Independent draws can repeat when sampling is with replacement. That is the usual default on a simple button and the default of numpy.random.choice when replace is left true. Two “heads” in a row can be honest. If you need unique options, sample without replacement, and do not ask for more results than the list holds. An empty list cannot yield a choice. Neither a coin nor a library will mint a face that is not there.

Weights are optional. Equal chance is the honest reading when you did not supply weights. Duplicate lines are accidental weights. A probability array, when you use one, must match the options one-for-one and describe a real distribution. If you did not want option C to be rare, do not mark it rare and then blame the draw.

np random choice on an array

People type np random choice when they want the NumPy call. numpy.random.choice takes the array (or an integer n, meaning the range 0 through n minus one), an optional size, a replace flag, and an optional p for probabilities. numpy.random.Generator.choice is the newer generator API with the same job on a Generator object. Both pick from what you passed. Neither one reads a spreadsheet you forgot to load.

a: the options, or an integer standing for arange(n). size: how many draws; omit it for one value. replace: true allows repeats; false deals unique values. p: optional weights, one per option. Choosing from strings works when the array is strings: names, labels, file paths. Choosing from integers is still a choice among numbers, not among people, until you keep a legend. Encoding five teammates as 0–4 and then losing the legend is how teammate five never plays. Pass the names if the names are the options. Print the array beside the result if a colleague must replay the script.

Build the array of real options. Set replace to match the rules: stream or deal. Set size to the number of results you need. If you use p, check it against the array length before you draw. Record the array, the flags, and the output.

A choice is not a number range in disguise

A giveaway with consecutive ticket numbers can use an integer range. A giveaway with named prizes, skipped rows, or people, should use this picker. Translating names into integers and back is extra surface for an off-by-one. The random choice generator exists so you can skip that costume. In a meeting, show the list, then the pick. In code, keep the script. A screenshot of “B” with no list is not a specification.

numpy.random.choice will not choose if the array is empty. A fair coin with no faces has the same problem. Do not use an ordinary choice widget for a secret token. Do not use it as a substitute for a decision you were already going to make; if you will veto every result you dislike, skip the ceremony and pick. Taste is allowed when the list is a prompt. Taste is not allowed when you already told a room the draw would stand.

Questions and answers

What does np random choice mean?
It is the NumPy function numpy.random.choice, which draws from an array (or from arange(n) if you pass an integer n), with optional size, replace, and weights.
Can I draw more items than the list holds?
Yes with replacement. No without replacement: the deal cannot be longer than the array.
Why did one name win so often?
Duplicates, weights, or a short list with replacement. Equal lines with replace true can still clump. That can be honest.

Related guides

Tools

Address random generator

An address random generator builds a fake street line so you do not invent one by hand. Searches for random address generator, address generator random, and a New York variant still want a sample, not

Tools

Alphabet random generator

An alphabet random generator shuffles the letters of an alphabet into a new order. For English that is a permutation of A through Z, each letter once.

Tools

Animal generator random

An animal generator random pick is one animal name drawn from the tool’s list. Each click returns one animal from that list, not a living creature and not a complete catalog of Earth.

All guides

All pages