Hummus
Hummus

Reputation: 365

Randint error in Python?

I'm writing up the code for the RSA algorithm as a project. With those who are familiar woth this encryption system,the function below calculates the value of phi(n). However when I run this, it comes up this error:

    Traceback (most recent call last):
  File "C:\Python27\RSA.py", line 127, in <module>
    phi_n()
  File "C:\Python27\RSA.py", line 30, in phi_n
    prime_f = prime_list[random.randint(0,length)]
  File "C:\Python27\lib\random.py", line 241, in randint
    return self.randrange(a, b+1)
  File "C:\Python27\lib\random.py", line 217, in randrange
    raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
ValueError: empty range for randrange() (0,0, 0)

I don't fully understand why this error is coming up. Here is my code for the phi_n function:

def phi_n():  
    global prime_list
    length = len(prime_list) - 1
    prime_f = prime_list[random.randint(0,length)]
    prime_s = prime_list[random.randint(0,length)]
    global pq
    n = prime_f * prime_s
    global phi_n
    phi_n = (prime_f -1) * (prime_s -1)
    return phi_n

Comment will be appreciated.

Thanks

Upvotes: 0

Views: 407

Answers (1)

Rob Wouters
Rob Wouters

Reputation: 16327

The problem is prime_list is empty and therefore length will equal -1 resulting in the call random.randint(0, -1) which is invalid for obvious reasons.

Upvotes: 5

Related Questions