views:

432

answers:

4

Hi,

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

+1  A: 

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:

randomInts = [random.randrange(2000) for _ in range(100)]

To convert to strings:

randomStrings = [str(x) for x in randomInts]
Mark Byers
-1: `random.sample()` doesn't generate an entire list to sample from. `random.sample(xrange(10**9), 100)` and `random.sample(xrange(10**3), 100)` take the same time.
J.F. Sebastian
OK thanks for telling me! I didn't realize it could optimize through a generator and not actually generate the elements. It makes me wonder how it knows that what numbers it will get in the future without actually generating them all. Generators can return anything they want. It must be some special handling specifically for xrange?
Mark Byers
It is better just to look at the source of `random.sample`, but I think it uses just `__len__` and `__getitem__` methods.
J.F. Sebastian
A: 
import random
r = random.sample(range(2000), 100)
# One way to convert them to strings:
s = [str(x) for x in r]
Brian Neal
A: 

If you'll be generating lots and lots of lists of random numbers, you can pre-generate the strings to accelerate the lookup, something like this.

stringcache = dict((val, str(val)) for val in range(2000))

while some_condition: 
    r_strings = map(stringcache.get, random.sample(range(2000), 100))
TokenMacGuy
+2  A: 

Use xrange instead of range:

lst = random.sample(xrange(10**9), 100)

To convert to a list of strings:

strings = map(str, lst)

As one string:

s = ''.join(strings)
J.F. Sebastian
xrange doesn't exist in Python 3.0. "NameError: name 'xrange' is not defined"
Mark Byers
@Mark: *Most* of the code tagged `python` doesn't work on Python 3.0 (due to trivial differences `print` statement -> `print()` function, `xrange` -> `range`. Unless the question *explicitly* states Python 3.x requirement all code *should* be assumed to be Python 2.x
J.F. Sebastian