How to make a list of n numbers in Python and randomly select any number?

Question:

I have taken a count of something and it came out to N.

Now I would like to have a list, containing 1 to N numbers in it.

Example:

N = 5

then, count_list = [1, 2, 3, 4, 5]

Also, once I have created the list, I would like to randomly select a number from that list and use that number.

After that I would like to select another number from the remaining numbers of the list (N-1) and then use that also.

This goes on it the list is empty.

Asked By: Sunny

||

Answers:

You can try this code

import random
N = 5
count_list = range(1,N+1)
random.shuffle(count_list)

while count_list:
    value = count_list.pop()
    # do whatever you want with 'value'
Answered By: user948581

Create the list (edited):

count_list = range(1, N+1)

Select random element:

import random
random.choice(count_list)
Answered By: pvoosten

As for the first part:

>>> N = 5
>>> count_list = [i+1 for i in xrange(N)]
>>> count_list
[1, 2, 3, 4, 5]
>>>

As for the second, read 9.6. random — Generate pseudo-random numbers.

>>> from random import choice
>>> a = choice(count_list)
>>> a
1
>>> count_list.remove(a)
>>> count_list
[2, 3, 4, 5]

That’s the general idea.

By the way, you may also be interested
in reading Random selection of elements in a list, with no repeats (Python recipe).

There are a few implementations of fast random selection.

Answered By: Gandi

You can create the enumeration of the elements by something like this:

mylist = list(xrange(10))

Then you can use the random.choice function to select your items:

import random
...
random.choice(mylist)

As Asim Ihsan correctly stated, my answer did not address the full problem of the OP. To remove the values from the list, simply list.remove() can be called:

import random
...
value = random.choice(mylist)
mylist.remove(value)

As takataka pointed out, the xrange builtin function was renamed to range in Python 3.

Answered By: Constantinius

You don’t need to count stuff if you want to pick a random element. Just use random.choice() and pass your iterable:

import random
items = ['foo', 'bar', 'baz']
print random.choice(items)

If you really have to count them, use random.randint(1, count+1).

Answered By: patrys

You can use:

import random
random.choice(range(n))

or:

random.choice(range(1,n+1))

if you want it from 1 to n and not from 0.

Answered By: Guy

After that I would like to select another number from the remaining numbers of the list (N-1) and then use that also.

Then you arguably do not really want to create a list of numbers from 1 to N just for the purpose of picking one (why not just ask for a random number in that range directly, instead of explicitly creating it to choose from?), but instead to shuffle such a list. Fortunately, the random module has you covered for this, too: just use random.shuffle.

Of course, if you have a huge list of numbers and you only want to draw a few, then it certainly makes sense to draw each using random.choice and remove it.

But… why do you want to select numbers from a range, that corresponds to the count of some items? Are you going to use the number to select one of the items? Don’t do that; that’s going out of your way to make things too complicated. If you want to select one of the items, then do so directly – again with random.choice.

Answered By: Karl Knechtel

Maintain a set and remove a randomly picked-up element (with choice) until the list is empty:

s = set(range(1, 6))
import random

while len(s) > 0:
  s.remove(random.choice(list(s)))
  print(s)

Three runs give three different answers:

>>>
set([1, 3, 4, 5])
set([3, 4, 5])
set([3, 4])
set([4])
set([])
>>>
set([1, 2, 3, 5])
set([2, 3, 5])
set([2, 3])
set([2])
set([])

>>>
set([1, 2, 3, 5])
set([1, 2, 3])
set([1, 2])
set([1])
set([])
Answered By: kiriloff
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.