Random module not working. ValueError: empty range for randrange() (1,1, 0)
Solution 1
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()
Solution 2
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.
Solution 3
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
Solution 4
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
Related videos on Youtube

user551717
Updated on July 09, 2022Comments
-
user551717 6 months
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?
-
Admin about 12 yearsPro 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). -
Lennart Regebro about 12 yearsPlease don't claim module isn't working before reading the documentation.
-
John Machin about 12 yearsPro 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.
-
-
bgporter about 12 yearsNote that the OPs exception is raised whenever
b < a
-
user551717 about 12 yearsI know that's how you call it. I'm calling it and it throws an error because the random module is corrupt.
-
Rafe Kettler about 12 years@user551717 then you should do a diff between your version of the model and the source I posted and see what's up
-
Lennart Regebro about 12 yearsRight, but (1,1, 0) is (a,b+1, b-a+1), so in this case he called it with a=1 and b=0.
-
user551717 about 12 yearsokay, thank you. I forgot to add a person to the list of which the length is a before I called it.
-
Lennart Regebro about 12 years@user551717: So you are gonna select one item from the list randomly? There is already a function for that in the random module. Please read the documentation. docs.python.org/library/random.html Also note that if you add to the second parameter, randint will give you a random integer between 1 and 1. Ie, it will always return 1.