-1

I want to do a random pick from an array but not have the same number come up twice.

array = ['1','2','3','4',]
random.choice(array)

Right now it just picks a number, is there another way so that it will randomize but only do each number once?

jgritty
  • 11,660
  • 3
  • 38
  • 60
user-313
  • 7
  • 4
  • just do `random.choice(array,size=3,replace=False)`if you want 3 different numbers for example – fmarm Oct 22 '19 at 22:54
  • 2
    @fmarm Where did you get that usage from? It's not mentioned on https://docs.python.org/3/library/random.html – mkrieger1 Oct 22 '19 at 22:57
  • @mkrieger, sorry, I am using the function `random.choice` from numpy, you should do `import numpy as np; np.random.choice(array,size=3,replace=False)`. Doc https://docs.scipy.org/doc/numpy-1.16.0/reference/generated/numpy.random.choice.html – fmarm Oct 22 '19 at 23:03

1 Answers1

1

A really easy way to do this would be to remove the choice from the list, after you choose it.

my_array = ['1','2','3','4']
x = random.choice(my_array)
my_array.remove(x)
jgritty
  • 11,660
  • 3
  • 38
  • 60
  • Thanks for the response, i put: ``` lottery = ['4753672', '3963852', '3092670', '9346897', '1234567', ] x = random.choice(lottery)``` and it prints x and then ```lottery.remove(x)``` this gives me the error "ValueError: list.remove(x): x not in list" – user-313 Oct 22 '19 at 23:49
  • You need to assign a value to x like this `x = random.choice(my_array)` – jgritty Oct 23 '19 at 00:12
  • haha sorry i did that it just didn't format properly in the thing above. i have x = random.choice(lottery) (lottery is the array) – user-313 Oct 23 '19 at 00:23