How to generate a random (but unique and sorted) list of a fixed given length out of numbers of a given range in Python?
Something like that:
>>> list_length = 4 >>> values_range = [1, 30] >>> random_list(list_length, values_range) [1, 6, 17, 29] >>> random_list(list_length, values_range) [5, 6, 22, 24] >>> random_list(3, [0, 11]) [0, 7, 10]
Use randint() when you want to generate a random number from an inclusive range. Use randrange() when you want to generate a random number within a range by specifying the increment.
The random. uniform() function returns a random floating-point number between a given range in Python. For example, It can generate a random float number between 10 to 100 Or from 50.50 to 75.5.
A random sample like this returns list of unique items of sequence. Don't confuse this with random integers in the range.
>>> import random >>> random.sample(range(30), 4) [3, 1, 21, 19]
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