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?
random.randint(1, 0)
returns an error because whenever you userandom.randint(a, b)
a must be less than b. Try changingrandom.randint(1, 0)
torandom.randint(0, 1)
to get a valid result.Click here for more information about
random.randint
If you called
randint()
by itself, it will most definitely result in an error. You need to providerandint()
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=markupYou called randint like this:
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:
Trust me,
random
works just fine. You are callingrandint
withb
<a
:randint
returns a value between the first argument and the second argument.