dimanche 18 décembre 2016

Why My Python Random's seed doesn't Work?

I am trying to implement an algorithm, and I build a class for it

class Ksampler:

    def __init__(self, K,N,L0):

        self.M31=2147483647
        self.L0=L0
        self.N=N
        self.error=self.prob=float(1)/N
        self.LNUM  =31
        #self.t = self.K = int(log(float(N),2))
        self.K=self.t=K
        self.M61 = 2305843009213693951
        self.Kmax=5*self.K
        self.n=pow(2,31)
        print 'k=',self.K,'l=',self.LNUM
        rn.seed()
        self.r=[int(rn.uniform(0,self.M61-1)) for i in range(self.t)]

        ......

Then I build many instances:

for i in range(100):
    #rn.seed(i)
    sam[i]=Ksampler(5,100,100)

But my output is wrong, so I print the random numbers:

def prin(self):
    print self.r

When I call this function, I found these 100 random numbers are just 10 or 9 groups of random numbers, not 100 groups. for example, those 4 groups random numbers are just the same one!

[2193020450880616192, 2067278345537971968, 1594906683693397504,     2120708475826965504, 756774356426756096]
[2193020450880616192, 2067278345537971968, 1594906683693397504, 2120708475826965504, 756774356426756096]
[2193020450880616192, 2067278345537971968, 1594906683693397504, 2120708475826965504, 756774356426756096]
[2193020450880616192, 2067278345537971968, 1594906683693397504, 2120708475826965504, 756774356426756096]

Why this happen? I use rn.seed() when each instance initialized.




Aucun commentaire:

Enregistrer un commentaire