dl4jnd4j

ND4J: Generate random number within range


If we want to create an INDArray of given rows and columns filled with random number between 0 and 1 we can use:

Nd4j.rand(row, cols);

But what if we want INDArray of given rows and columns filled with random number between -5000 and 5000? Moreover, what if each column / vector has random number selected from specific range? For example:

row = 2; columns = 3
pool = { {-500, 500}, {100, 100}, {1, 10} }
INDArray = [ [randRange(-500,500), randRange(100,100), randRange(1,10)],
             [randRange(-500,500), randRange(100,100), randRange(1,10)] ]

How can this be achieved by harnessing the efficiency of Nd4j?


Solution

  • Use other signatures.

    I.e this one:

    Nd4j.rand(long[] shape, double min, double max, org.nd4j.linalg.api.rng.Random rng);

    or this one:

    Nd4j.rand(INDArray target, double min, double max, org.nd4j.linalg.api.rng.Random rng);