0

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, but still gives a similar characteristic. How to solve it?

1 Answer 1

1

print is called in the outmost loop, so it gets only the last value of rand.

just print the rand value after each rand call, and you will see it is really working, and different each time.

 rand = np.random.rand() print(rand) 

You will get something like:

0.360516239461243 0.5027246719138412 0.5227674056389815 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 0.34277438417911266 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but now I have another best inspections, that is only place the 'print(rand)' after 'yards[i][j] = 0'
OK, so does it solves your problem ? or have you another issue with rand() ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.