我想做一個回傳偽亂數生成器的類。我想將 _rand 函式的結果保存在 _r 中,但是當我呼叫函式 _rand(int(time.time())) 時,我收到錯誤訊息“rand() 缺少 1 個必需的位置引數:'seed'”。有人可以解釋一下我做錯了什么。
class PRNG:
import time
_m = 32768
_b = 9757
_c = 6925
def _rand(self, seed):
n = seed % self._m
while True:
n = (n * self._b self._c) % self._m
yield n
_r = _rand(int(time.time()))
def rand(self) :
return self._r.__next__()
prng = PRNG()
print(prng.rand())
uj5u.com熱心網友回復:
_rand()需要兩個引數;self和seed。具體來說,您需要提供呼叫該方法的實體。嘗試_r在建構式中定義而不是你所擁有的
def __init__(self):
self._m = 32768
self._b = 9757
self._c = 6925
self._r = self._rand(int(time.time()))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/369838.html
