What is the best way of getting random numbers in NumPy?

后端 未结 4 1494
梦谈多话
梦谈多话 2020-12-29 23:01

I want to generate random numbers in the range -1, 1 and want each one to have equal probability of being generated. I.e. I don\'t want the extremes to be less

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-29 23:42

    Your approach is fine. An alternative is to use the function numpy.random.uniform():

    >>> numpy.random.uniform(-1, 1, size=10)
    array([-0.92592953, -0.6045348 , -0.52860837,  0.00321798,  0.16050848,
           -0.50421058,  0.06754615,  0.46329675, -0.40952318,  0.49804386])
    

    Regarding the probability for the extremes: If it would be idealised, continuous random numbers, the probability to get one of the extremes would be 0. Since floating point numbers are a discretisation of the continuous real numbers, in realitiy there is some positive probability to get some of the extremes. This is some form of discretisation error, and it is almost certain that this error will be dwarved by other errors in your simulation. Stop worrying!

提交回复
热议问题