Sorted list without sorting, O (n)
If you want integers to be sorted, I got this answer in another question with a lot of help. You can do this using an exponential variable and thereby avoid any kind . As a result, this is O (n):
From Alok's answer and Dan Dyer's comment, it turns out that using the exponential distribution for a set of deltas gives a uniform distribution of integers in a sequence.
So, you are just starting to generate numbers, and then scale them at the end. Adding 1 to the delta ensures that you will never repeat the value.
import random,sys,math def genSortedInts(mini,maxi,vals): running = 0 deltas = [random.expovariate(1.0) for i in range(0,vals+1)] floats = [] for d in deltas: running += d floats.append(running) upper = floats.pop() valRange = maxi-mini-(vals-1) ints = [mini+int(f/upper*valRange)+id for id,f in enumerate(floats)] return ints if __name__ == "__main__": vals = 10 maxi = 80 mini = 0 print(genSortedInts(mini,maxi,vals))
Note the use of random.expovariate(1.0) , a Python exponential random number generator (very useful!). Here it is called with a value of 1.0 (arg is 1 / mean), but since the script normalizes against the last number in the sequence, the value itself does not matter.
Conclusion (fair bone roll) for 10 values up to 80:
[3, 5, 10, 16, 25, 37, 41, 45, 57, 70]