randint 方法 returns 每次执行时的值相同
randint method returns the same value on each execution
在下面的代码中,我试图学习如何使用同步线程。如下代码所示,我每 3 秒生成一个线程,并将一个随机数传递给
方法 isNumPrime(...) 检查生成的数字是否为素数。
我面临的问题是
1-我每次收到的输出我 运行 代码是相同的。请查看下面的输出。每次按 ctrl+f5 都会生成相同的随机数
是
thread_1 = threading.Thread(group = None, target = self.isNumPrime,
name='Thread_1', args = (), kwargs=dict(targetNum=randint(0,100)),
daemon = None)
确实生成了随机数`??因为我得到的不是随机生成的数字。我希望每次按下时都会收到不同的生成数字
ctrl+f5
2-给定输出 belwo,为什么我收到重复输出“in loop: targetNum: xx”?每次迭代都必须显示一次,其中的数字范围为
(3,targetNum)
代码:
import threading
import logging
import time
from random import seed
from random import randint
class ThreadsWithSync(threading.Thread):
def __new__(cls):
"""
For object creation
"""
print("cls: %s"%(cls))
#cls.onCreateObject()
instance = super(ThreadsWithSync, cls).__new__(cls)
#print("instace: %s"%(instance.__repr__)) #activate this line whenever an informative and descriprtive text about the instance is needed to be displayed
return instance
def __init__(self):
"""
For object initialization
"""
#print("self: %s"%(self))
threading.Thread.__init__(self) #to initialize the super class
print("self: %s"%(self))
seed(1)
@classmethod
def onCreateObject(cls):
"""
This will be invoked once the creation procedure of the object begins.
"""
def __repr__(self):
"""
similar to toString in Java
"""
return "\n__class__: " + repr(self.__class__) +"\n__new__:" + repr(self.__new__) + "\n__str__: " + repr(self.__str__) + "\n__sizeof__: " + repr(self.__sizeof__)
def isNumPrime(self, targetNum):
if targetNum == 0 or targetNum == 1:
print("targetNum passed to method, will return false: %s"%(targetNum))
return False
if targetNum == 2:
print("targetNum passed to method will return true: %s"%(targetNum))
return True
isPrim = True
for i in range(3,targetNum):
print("in loop: targetNum: %s"%(targetNum))
if targetNum % i == 0:
isPrim = False
break
print("is %s a prime : %s"%(targetNum, isPrim))
return isPrim
def spawnThread(self):
if __name__ == "__main__":
self.main()
def main(self):
while True:
thread_1 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_1', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)
#thread_2 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_2', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)
thread_1.start()
#thread_2.start()
time.sleep(3)
t1 = ThreadsWithSync()
t1.spawnThread()
输出:
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
is 17 a prime : True
in loop: targetNum: 72
is 72 a prime : False
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
is 97 a prime : True
in loop: targetNum: 8
in loop: targetNum: 8
is 8 a prime : False
in loop: targetNum: 32
in loop: targetNum: 32
is 32 a prime : False
in loop: targetNum: 15
is 15 a prime : False
in loop: targetNum: 63
is 63 a prime : False
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
...
...
...
...
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
is 97 a prime : True
in loop: targetNum: 57
由于您以相同的方式初始化种子,所以每次都会得到相同的数字。完全不要调用 seed(1)
。
至于每次打印 targetNum - 也许你不应该在循环中打印它。
正如其他人所说,从您的 seed()
调用中删除 1
。将其留空 seed()
.
至于为什么会重复targetNum
,是因为你让它打印targetNum
。 :) 尝试打印 i
代替:
print("in loop: targetNum: %s"%(i))
在下面的代码中,我试图学习如何使用同步线程。如下代码所示,我每 3 秒生成一个线程,并将一个随机数传递给 方法 isNumPrime(...) 检查生成的数字是否为素数。 我面临的问题是
1-我每次收到的输出我 运行 代码是相同的。请查看下面的输出。每次按 ctrl+f5 都会生成相同的随机数 是
thread_1 = threading.Thread(group = None, target = self.isNumPrime,
name='Thread_1', args = (), kwargs=dict(targetNum=randint(0,100)),
daemon = None)
确实生成了随机数`??因为我得到的不是随机生成的数字。我希望每次按下时都会收到不同的生成数字 ctrl+f5
2-给定输出 belwo,为什么我收到重复输出“in loop: targetNum: xx”?每次迭代都必须显示一次,其中的数字范围为 (3,targetNum)
代码:
import threading
import logging
import time
from random import seed
from random import randint
class ThreadsWithSync(threading.Thread):
def __new__(cls):
"""
For object creation
"""
print("cls: %s"%(cls))
#cls.onCreateObject()
instance = super(ThreadsWithSync, cls).__new__(cls)
#print("instace: %s"%(instance.__repr__)) #activate this line whenever an informative and descriprtive text about the instance is needed to be displayed
return instance
def __init__(self):
"""
For object initialization
"""
#print("self: %s"%(self))
threading.Thread.__init__(self) #to initialize the super class
print("self: %s"%(self))
seed(1)
@classmethod
def onCreateObject(cls):
"""
This will be invoked once the creation procedure of the object begins.
"""
def __repr__(self):
"""
similar to toString in Java
"""
return "\n__class__: " + repr(self.__class__) +"\n__new__:" + repr(self.__new__) + "\n__str__: " + repr(self.__str__) + "\n__sizeof__: " + repr(self.__sizeof__)
def isNumPrime(self, targetNum):
if targetNum == 0 or targetNum == 1:
print("targetNum passed to method, will return false: %s"%(targetNum))
return False
if targetNum == 2:
print("targetNum passed to method will return true: %s"%(targetNum))
return True
isPrim = True
for i in range(3,targetNum):
print("in loop: targetNum: %s"%(targetNum))
if targetNum % i == 0:
isPrim = False
break
print("is %s a prime : %s"%(targetNum, isPrim))
return isPrim
def spawnThread(self):
if __name__ == "__main__":
self.main()
def main(self):
while True:
thread_1 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_1', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)
#thread_2 = threading.Thread(group = None, target = self.isNumPrime, name='Thread_2', args = (), kwargs=dict(targetNum=randint(0,100)), daemon = None)
thread_1.start()
#thread_2.start()
time.sleep(3)
t1 = ThreadsWithSync()
t1.spawnThread()
输出:
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
in loop: targetNum: 17
is 17 a prime : True
in loop: targetNum: 72
is 72 a prime : False
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
is 97 a prime : True
in loop: targetNum: 8
in loop: targetNum: 8
is 8 a prime : False
in loop: targetNum: 32
in loop: targetNum: 32
is 32 a prime : False
in loop: targetNum: 15
is 15 a prime : False
in loop: targetNum: 63
is 63 a prime : False
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
...
...
...
...
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
in loop: targetNum: 97
is 97 a prime : True
in loop: targetNum: 57
由于您以相同的方式初始化种子,所以每次都会得到相同的数字。完全不要调用 seed(1)
。
至于每次打印 targetNum - 也许你不应该在循环中打印它。
正如其他人所说,从您的 seed()
调用中删除 1
。将其留空 seed()
.
至于为什么会重复targetNum
,是因为你让它打印targetNum
。 :) 尝试打印 i
代替:
print("in loop: targetNum: %s"%(i))