4

Is there a function in Python (I'm working with SageMath) to get 10 random integers between 0 and 30, for instance, without repeating them?

martineau
  • 119,623
  • 25
  • 170
  • 301
vandermies
  • 183
  • 3
  • 14
  • 2
    look it up here: http://stackoverflow.com/questions/3996904/generate-random-integers-between-0-and-9 – dima Oct 13 '16 at 18:07
  • @dima: For the record, this is not a duplicate of [Generate random integers between 0 and 9](http://stackoverflow.com/q/3996904/364696), because generating multiple unique values is a meaningfully different problem. That said, [Generate a set of sorted random numbers from a specific range](http://stackoverflow.com/q/14748910/364696) is identical, except it also wants the values sorted (and that step can be omitted easily). – ShadowRanger Oct 13 '16 at 18:32

2 Answers2

10
import random
random.sample(range(31), 10) 
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 1
    `def gen_between(start, end, count): return random.sample(range(start, end+1), count)` would help calling this a bit more customizably. – Adam Smith Oct 13 '16 at 18:13
  • 1
    random.sample(range(30+1), 10), actually. – astraujums Oct 13 '16 at 18:16
  • @AdamSmith it hardly seems worth it. For one thing it forces us to care about `start` and `count`, which we probably won't a lot of the time – Patrick Haugh Oct 13 '16 at 18:17
  • Huh. I loaded this question with only one (bad) answer, made my own in 2-3 minutes, and when I post and reload, you already gave it. Weird. In any event, adding one note from my answer before deleting: On Py2, use `xrange`, not `range`, to avoid creating a temporary `list` (Py3's `range` is already a "lazy sequence"). Also, [link to `random.sample` docs](https://docs.python.org/3/library/random.html#random.sample) for reference. And as astraujums noted, if range from 0 to 30 is supposed to be inclusive, use `range(31)` to allow `30` as a legal value. – ShadowRanger Oct 13 '16 at 18:17
  • @astraujums Good point. – Patrick Haugh Oct 13 '16 at 18:18
  • That works well for 0 to 31, but what if the range is 0 to 1000000000? This would fill RAM with gigabytes of numbers just to pick one of them. And there is already `random.randint` which does the job correctly. – zvone Oct 13 '16 at 18:25
  • 1
    @zvone only if you are in python 2, in which case you should use xrange as ShadowRanger and the documentation said – Copperfield Oct 13 '16 at 18:29
  • @zvone In Python 3, `range` objects are special sequences that handle cases like this very well. Use `xrange` in Python 2. `random.randint` has a different functionality. I suggest you carefully read the question and soem of the other answers here to see how that solution may have problems. – Patrick Haugh Oct 13 '16 at 18:29
  • @PatrickHaugh Good point there, I did not know `xrange` would work with this. – zvone Oct 13 '16 at 18:34
  • 3
    @zvone: A lot of people assume `xrange` returns a generator object, when in fact it returns an object that mostly implements the sequence protocol (and is therefore iterable), and for `random.sample`, all that matters is that you can call `len` on it and index it by integer index. Py3's `range` is just an improved version of Py2's `xrange` that supports the full sequence protocol (allows slicing, `O(1)` membership testing, etc.). – ShadowRanger Oct 13 '16 at 18:36
0
>>> n=[]
>>> while len(n)<=10:
    x=randint(0,30)
    if x not in n:
        n.append(x)

do not forget to clear list every time you run this code

Hisham Karam
  • 1,288
  • 17
  • 28