Search code examples
pythonnumpyrandomsequential-number

Generate sequential numbers between 0 and 1 with equal distance


I am generating random variables in my array: np.random.rand(5,3,20)

How can I create the same shape and size but sequentially between 0 and 1?


Solution

  • Create evenly spaced numbers over a specified interval using linspace and then reshape to shape required as follows:

    np.linspace(0, 1, 300).reshape(5, 3, 20)
    

    Note:

    'The new shape should be compatible with the original shape'

    So let's say, for np.linspace(0, 1, t).reshape(x, y, z) the condition that should be met is t = x*y*z