mercredi 16 mars 2016

"Updating" the rng in python

I have to iterate an operation over several sets of partially randomized copies of an initial array of 0 and 1. I would like the copies to be different, of course, but also the sets.

For now I use this code (I omitted certain parts that should not interfere in the problem) :

def randomizer(b) :
"randomizes a fraction rate of b"

    c = np.copy(b)
    num_elem = len(c)
    idx = np.random.choice(range(num_elem), int(num_elem*rate), replace=False)
    c[idx] = f(c[idx])
    return c

def randomizePatterns(pattern, randomizer) :
"""Return nbTrials partially randomized copies of the given input pattern"""
    outputs = np.tile(pattern,(nbTrials,1))   
    for line in xrange(nbTrials) :       
        outputs[line] = randomizer(outputs[line,:])   
    return outputs

def test(pattern,randomizer) :
    randomPatterns = randomizePatterns(pattern, randomizer)
    "do something with them"
    return bool

def metaTest(pattern):       
    successNumber = 0   

    for plop in xrange(10):                 
        if test(pattern, randomizer) :           
        successNumber += 1
    return successNumber

When I run metaTest, I always get either 0 or 10, never an intermediate value (considering the inputs, that are also random but much more complicated, and the nature of test, I expect something about 5. I get each result approximatively half of the time).

Edit : To be more precise, printing the random patterns after each incrementation of plop, I get the same thing ten times, and I would like that to change

I hope this is not a stupid question, and thanks in advance for your help




Aucun commentaire:

Enregistrer un commentaire