import time
class Util(object): def __init__(self): self.index = 624 self.MT = [0] * 624
def inter(self,t): return (0xFFFFFFFF & t) # 取最后32位->t def twister(self): for i in range(self.index): y = self.inter((self.MT[i] & 0x80000000) + (self.MT[(i + 1) % self.index] & 0x7fffffff)) self.MT[i] = self.MT[(i + 397) % self.index] ^ y >> 1 if y % 2 != 0: self.MT[i] = self.MT[i] ^ 0x9908b0df self.index = 0 def exnum(self): if self.index >= 624: self.twister() y = self.MT[self.index] y = y ^ y >> 11 y = y ^ y << 7 & 2636928640 y = y ^ y << 15 & 4022730752 y = y ^ y >> 18 self.index = self.index + 1 return self.inter(y) def mainset(self,seed): self.MT[0] = seed # seed for i in range(1, self.index): self.MT[i] = self.inter(1812433253 * (self.MT[i - 1] ^ self.MT[i - 1] >> 30) + i) return self.exnum() def randomint(self,min_num,max_num): try: so = float(self.mainset(int(time.time()))) / (2 ** 32 - 1) rd = min_num + int((max_num - min_num) * so) return rd except Exception as ex: print "randomint function excute exception:" + str(ex)
原文地址:https://www.cnblogs.com/chmyee/p/9924195.html
时间: 2024-11-10 07:34:27