Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate list of random integers, but only using specified integers? (Python) [duplicate]

This question could be generalized to randomly creating a list of size n, containing pre-specified integers (assuming a uniform distribution). Hopefully it would look like the following:

Perhaps the syntax would look something like

randList([list of integers], size)

which could then produce something along the lines of:

randList([1,-1], 7)
>>> [1,-1,1,-1,-1,-1,1] #(random)

or

randList([2,3,4], 10)
>>> [4,3,4,2,2,4,2,3,4,3] #(random)

I am a bit new to python and everywhere I look it is always returning a range of values between a low and a high, and not specific values. Thanks

like image 837
J. Davis Avatar asked Sep 29 '15 20:09

J. Davis


People also ask

How do you generate a list of random numbers without duplicates or repeats in Python?

Using randint() & append() functions Use the randint() function(Returns a random number within the specified range) of the random module, to generate a random number in the range in specified range i.e, from 1 to 100.


1 Answers

vals = [random.choice(integers) for _ in range(num_ints)]
like image 131
acushner Avatar answered Nov 14 '22 23:11

acushner