#------------------------------------------------------------------------------ # RandomInts2.py #------------------------------------------------------------------------------ import random rng = random.Random() # create a random number generator def randomList2(num, lower_bound, upper_bound): """ Generate a list containing num random ints between lower_bound (inclusive) and upper_bound (exclusive). The result will contain no duplicates. """ if num>(upper_bound-lower_bound): return None # end if result = [] for i in range(num): while True: candidate = rng.randrange(lower_bound, upper_bound) if candidate not in result: break # end if # end while result.append(candidate) # end for return result # end randomList2() #-- main ---------------------------------------------------------------------- L = randomList2(5, 1, 13) print(L) # print some more random lists print(randomList2(10,1,6)) print(randomList2(10,1,11)) print(randomList2(10,1,21))