AttributeError: 'builtin_function_or_method' object has no attribute 'randint'

Viewed 3456

I'm doing a mockup of an RSA Key generation, however in my python code I keep getting this traceback

Traceback (most recent call last):

File "main.py", line 88, in

e= random.randint(1, phi_n - 1)

"AttributeError: 'builtin_function_or_method' object has no attribute 'randint'"

#RSA Key generation
p=10901009301486216783837946716092058424135573374664735347830598196173785586611407261441711670705344256295673923919130212129440261694153470133415776192580387
q=8832596322191268124023251309111139917569700077420384757505252214138594117132436575010876851227478058236456118017778016128118196705270782663559340597535529
n= p * q
phi_n= (p-1) * (q-1)
e= random.randint(1, phi_n - 1)
while((EucAlgo(e,phi_n)) !=1):
  e = random.randint(1, (phi_n-1))
d= ExEucAlgo_modInverse(e,phi_n)
print(f"Kpr={d}")
print(f"Kpub=(n={n}) \n e={e}")
```[Code can be seen here][1]


  [1]: https://i.stack.imgur.com/24tuW.png
2 Answers

This can occur probably because you have another variable named random or you have not imported it properly.

Try checking your code over to see what else you assigned random to by doing,

print(random, type(random))

You can also try to import it like,

import random as rand
y = rand.randint(1,10) #Use it like this

Since you are directly importing the functions from the module 'random', you don't have to call them like module_name.function_name

from random import random, randrange, getrandbits, randint 
y = randint(1,10) 

Simply change random.randint to just randint

Related