jeudi 4 mai 2017

random.randint(0,1) does not seem to be 50-50 in code

A program I'm trying to use to test small cases of a math program is supposed to take a list of pairs [i,j] and randomly flip some of them to [j,i]. There are nn=n*(n-1)/2 pairs, and a test to flip each one. So for n=7, there's nn=21 pairs, so I should be seeing 10 or 11 flips.

Looking at the edge lists, I noticed only one of the edges were flipped. I started adding a "print" for debugging to print if it flips an edge, and running it over and over again it only flips ONE or ZERO of the pairs every time.

Just repeatedly executing random.randint(0,1) works fine. Why isn't this working out?

Here's the very simple code section

for i01 in range(nn):
    a=e[i01]
    #random.seed(time.time())
    flip = random.randint(0,1)
    if flip==1:
        print "flip"
        e[i01]=[a[1],a[0]]

I've tried it with or without changing the seed every time. It doesn't change. Each time I run the block, it only prints "flip" once or never.




Aucun commentaire:

Enregistrer un commentaire