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)
Sign up to request clarification or add additional context in comments.

3 Comments

+1. From the docs: This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.
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.
@sberry2A: random.randrange(100, 20001, 100) is clearer than random.randint(1, 2001) * 100. Performance should be similar because neither generates a sequence.
7

For Python 3:

import random

random.choice(range(100, 20100, 100))

14 Comments

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.
@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.
@Tom Zych: Based on Mark Dickenson's comment your answer doesn't work on Python 3 which returns an iterator for range.
@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, @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.
|
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.

Comments

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.