簡體   English   中英

randint 如何在 python 中工作以供 rsa 使用?

[英]How does the randint work in python for rsa use?

你好,有人可以解釋我為什么在嘗試使用 gcd 函數時出現錯誤...

這是我的代碼:

import random
n1 = 544
r = random.randint(2,100)
while True:
    if gcd(r,n1) == 1:
        break
    else:
        r+=1
e = r
print e

錯誤是:

Traceback (most recent call last):
  File "C:/Python27/oooo.py", line 8, in <module>
   if gcd(r, n1) == 1:
NameError: name 'gcd' is not defined

您只需要使用庫中的 gcd 函數:

import random
from fractions import gcd
n1 = 544
r = random.randint(2,100)
while True:
    if gcd(r,n1) == 1:
        break
    else:
        r+=1
e = r
print e

它會起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM