mardi 24 novembre 2020

Seem failure to generate random numbers

I have inspect about generates of random number. You can see that random numbers is generated four times in the nested loop:

prob_burn = 0.1
for times in range(20):
        
    for i in range(1, row-1):
        for j in range(1, col-1):

            if yards[i][j] == 2:
                rand = np.random.rand() 
                if yards[i+1][j] == 1: # South
                    if rand < prob_burn:
                        yards[i+1][j] = 2
                    else:
                        yards[i+1][j] = 1

                rand = np.random.rand()        
                if yards[i][j+1] == 1: # East
                    if rand < prob_burn:
                        yards[i][j+1] = 2 
                    else:
                        yards[i][j+1] = 1 

                rand = np.random.rand()        
                if yards[i-1][j] == 1: # North
                    if rand < prob_burn:
                        yards[i-1][j] = 2
                    else:
                        yards[i-1][j] = 1

                rand = np.random.rand()
                if yards[i][j-1] == 1: # West
                    if rand < prob_burn:
                        yards[i][j-1] = 2 
                    else:
                        yards[i][j-1] = 1

                yards[i][j] = 0
            else:
                continue
    print(rand)

when the 'yards' multidimensional array code is:

row = 42
col = row

yards = [[0 for i in range(col)] for j in range(row)]
prob_tree = 0.8 

for i in range(1, row-1):
    for j in range(1, col-1):
        rand = np.random.rand()
        if rand < prob_tree:
            yards[i][j] = 1
        else:
            yards[i][j] = 0

yards[6][6] = 2

I have been confused when compiled this. Its gives the output:

0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566
0.9858723195931566

That's seem not really generating random of numbers between [0,1]. I have already tried to growed that 'times' loop more than 20 and 'prob_burn' values, but still gives a similar characteristic. How to solve it?




Aucun commentaire:

Enregistrer un commentaire