I have a list:
> S = [1,8,93,3,8]
I need to pick a random number that is not in the list but within the max range of value. I am more concerned about time complexity O(n). The S could be a pretty big list.
import random
S=[1,8,93,3,8]
m = max(S)
for x in xrange(m):
rand = random.randint(1,m)
if rand not in S:
print rand
else:
print "Number - %d found in the array" % rand
break
I did not try list comprehension
Just ripped something out of a project of mine
Sketchy solution but it works.
If the
list
consists of integers and any number is acceptable:If the number must be an integer:
If the number must be an arbitrary integer between the largest and smallest elements in the
list
:If the number must be a psuedorandom integer between the largest and smallest elements in the
list
:This is the simplest thing I could come up with: