user63503 user63503 - 1 year ago 123
Python Question

how in python to generate a random list of fixed length of values from given range?

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)


>>>> random_list(list_length,values_range)


>>>> random_list(3,[0,11])


Answer Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download