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
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.
vals = [random.choice(integers) for _ in range(num_ints)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With