I am using this line of code to generate a random list of integers:
random.sample(range(2000), 100)
With this code i know i wont have double value’s with my result. Is their maybe a faster way to achieve the same results?
Now i actually have to convert these int to string. Whats the fastest way to do this?
Thanks
random.sample chooses integers from the list without replacement. If you are trying to avoid duplicates then what you are doing is the right method. Could your numbers get much larger? You need to use Python 3, or xrange in Python 2 to avoid generating the entire list of integers in the range.
(Thanks to J.F. Sebastian for pointing out that random.sample does NOT have to generate all integers if you use xrange.)
If you want to allow duplicates you can use randrange instead:
To convert to strings: