Be careful with random generate number

ROBIN DONG 2021-05-27 13:50

This is the program I have used for a month:

import numpy as np

np.random.seed(202105)
rand = np.random.rand()

# business logic code using 'rand'

Then I add anothernp.random.rand()in the head of the code, and this time the output data of this code became quite different.

The reason is simple: the “rand” always generates 0.9 in the previous execution, but since it becomes the second rand() call instead of the first one, it generates 0.06 (unfortunately). The 0.06 is far less than 0.9 so the output data is totally different.

I think the solution is also simple: don’t make your program too dependent on the random number, or just keep your generated number under a range (like 0.6~0.9 in my case).

[返回] [原文链接]