Generate random array of floats between a range

前端 未结 8 1797
孤独总比滥情好
孤独总比滥情好 2020-12-02 15:04

I haven\'t been able to find a function to generate an array of random floats of a given length between a certain range.

I\'ve looked at Random sampling but no funct

8条回答
  •  生来不讨喜
    2020-12-02 15:41

    There may already be a function to do what you're looking for, but I don't know about it (yet?). In the meantime, I would suggess using:

    ran_floats = numpy.random.rand(50) * (13.3-0.5) + 0.5
    

    This will produce an array of shape (50,) with a uniform distribution between 0.5 and 13.3.

    You could also define a function:

    def random_uniform_range(shape=[1,],low=0,high=1):
        """
        Random uniform range
    
        Produces a random uniform distribution of specified shape, with arbitrary max and
        min values. Default shape is [1], and default range is [0,1].
        """
        return numpy.random.rand(shape) * (high - min) + min
    

    EDIT: Hmm, yeah, so I missed it, there is numpy.random.uniform() with the same exact call you want! Try import numpy; help(numpy.random.uniform) for more information.

提交回复
热议问题