12

I have the following numbers:

100, 200, 300, 400 ... 20000

And I would like to pick a random number within that range. Again, that range is defined as 100:100:20000. Furthermore, by saying 'within that range', I don't mean randomly picking a number from 100->20000, such as 105. I mean randomly choosing a number from the list of numbers available, and that list is defined as 100:100:20000.

How would I do that in Python?

4 Answers 4

21

Use random.randrange :

random.randrange(100, 20001, 100)
3
  • 4
    +1. From the docs: This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.
    – sberry
    Sep 14, 2011 at 17:56
  • 1
    Out of curiosity I tried a benchmark of this answer vs. random.randint(1, 2001) * 100 which it turns out is pretty much the exact same. A difference of about 1 - 2 hundredths of a second for 1M iterations.
    – sberry
    Sep 14, 2011 at 18:03
  • @sberry2A: random.randrange(100, 20001, 100) is clearer than random.randint(1, 2001) * 100. Performance should be similar because neither generates a sequence. Sep 14, 2011 at 18:09
6

For Python 3:

import random

random.choice(range(100, 20100, 100))
14
  • 1
    Inefficient. From the docs for random.randrange: "Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object." Using xrange would not avoid this problem. random.choice must evaluate the entire iterator from xrange for its choice to be random. Sep 14, 2011 at 17:58
  • @Wooble: Nope. random.choice just generates a single random index and uses that to access the appropriate element of the sequence. Note that the argument to random.choice must support len and item access by index: random.choice doesn't accept arbitrary iterables. Sep 14, 2011 at 18:11
  • @Tom Zych: Based on Mark Dickenson's comment your answer doesn't work on Python 3 which returns an iterator for range. Sep 14, 2011 at 18:12
  • 1
    @Mark: Um, yes, it does. The docs say "iterable" and I tested it too. I agree it's probably less efficient than randrange, though.
    – Tom Zych
    Sep 14, 2011 at 18:14
  • 1
    @Tom, @Steven: Nowhere did I say Tom's answer doesn't work on Python 3. It does, precisely because range supports len and access by index. Sep 14, 2011 at 18:17
2

from python manual:

random.randrange([start], stop[, step])

Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.

1

This would do it

print random.choice(xrange(100, 20100, 100);

But this is probably better:

print int(round(random.randint(100, 200001),-2))

@mouad's answer looks the best to me.

EDIT
Out of curiosity I tried a benchmark of this answer vs. random.randint(1, 2001) * 100 which it turns out is pretty much the exact same. A difference of about 1 - 2 hundredths of a second for 1M iterations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.