14
votes

In Python 2.7.1, I import the random module. when I call randint() however, I get the error:

ValueError: empty range for randrange() (1,1, 0) 

This error is caused by an error in the random.py module itself. I don't know how to fix it, not does reinstalling python help. I can't change versions.

can someone please give me code for a working module or tell me what to do?

4
Can you post the complete and exact code that produces this error? - Mark Byers
Pro tip: Just because the random module is at the bottom of the stacktrace, it doesn't have to have caused the error (in fact, that's it's unlikely). As already said, post your code (including stacktrace). - user395760
Please don't claim module isn't working before reading the documentation. - Lennart Regebro
Pro tip: Read your stacktrace, your error message, your code, and The Fantastic Manual before you start blaming acts of (God, Gates, Guido, ...); the problem is much more likely to be an act of Goofy. - John Machin

4 Answers

21
votes

You called randint like this:

 randint(1,0)

That tells randint to return a value starting as 1 and ending at 0. The range of numbers from 1 to zero is as you surely realize an empty range. Hence the error:

 empty range for randrange()
7
votes

Trust me, random works just fine. You are calling randint with b < a:

>>> random.randint(1, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\random.py", line 228, in randint
    return self.randrange(a, b+1)
  File "C:\Python27\lib\random.py", line 204, in randrange
    raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop
, width)
ValueError: empty range for randrange() (1,1, 0)

randint returns a value between the first argument and the second argument.

1
votes

If you called randint() by itself, it will most definitely result in an error. You need to provide randint() with a range to choose from. randint(a, b), where a and b are integers, should work, and if it doesn't, your Python installation is broken.

It would also raise an exception if b is less than a. Think of it like you're supplying a range: it would make sense to put the lower bound first, right? So put the smaller bound first.

If you really want to compare your random module with the correct one, the source is at http://svn.python.org/view/python/branches/release27-maint/Lib/random.py?view=markup

1
votes

random.randint(1, 0) returns an error because whenever you use random.randint(a, b) a must be less than b. Try changing random.randint(1, 0) to random.randint(0, 1) to get a valid result.

Click here for more information about random.randint