use of org.nd4j.linalg.api.rng.Random in project nd4j by deeplearning4j.
the class RandomTests method testMultithreading2.
@Test
public void testMultithreading2() throws Exception {
final AtomicInteger cnt = new AtomicInteger(0);
final CopyOnWriteArrayList<INDArray> list = new CopyOnWriteArrayList<>();
Thread[] threads = new Thread[10];
for (int x = 0; x < threads.length; x++) {
list.add(null);
}
for (int x = 0; x < threads.length; x++) {
threads[x] = new Thread(new Runnable() {
@Override
public void run() {
Random rnd = Nd4j.getRandom();
rnd.setSeed(119);
INDArray array = Nd4j.getExecutioner().exec(new UniformDistribution(Nd4j.createUninitialized(25)));
Nd4j.getExecutioner().commit();
list.set(cnt.getAndIncrement(), array);
}
});
threads[x].start();
}
for (int x = 0; x < threads.length; x++) {
threads[x].join();
assertNotEquals(null, list.get(x));
if (x > 0) {
assertEquals(list.get(0), list.get(x));
}
}
}
use of org.nd4j.linalg.api.rng.Random in project nd4j by deeplearning4j.
the class RandomTests method testGaussianDistribution1.
@Test
public void testGaussianDistribution1() throws Exception {
Random random1 = Nd4j.getRandomFactory().getNewRandomInstance(119);
Random random2 = Nd4j.getRandomFactory().getNewRandomInstance(119);
INDArray z1 = Nd4j.create(100000);
INDArray z2 = Nd4j.create(100000);
INDArray zDup = z1.dup();
GaussianDistribution op1 = new GaussianDistribution(z1, 0.0, 1.0);
Nd4j.getExecutioner().exec(op1, random1);
GaussianDistribution op2 = new GaussianDistribution(z2, 0.0, 1.0);
Nd4j.getExecutioner().exec(op2, random2);
assertNotEquals(zDup, z1);
assertEquals(0.0, z1.meanNumber().doubleValue(), 0.01);
assertEquals(1.0, z1.stdNumber().doubleValue(), 0.01);
assertEquals(z1, z2);
}
use of org.nd4j.linalg.api.rng.Random in project nd4j by deeplearning4j.
the class RandomFactory method getNewRandomInstance.
/**
* This method returns new onject implementing Random interface, initialized with seed value
*
* @param seed seed for this rng object
* @return object implementing Random interface
*/
public Random getNewRandomInstance(long seed) {
try {
Random t = (Random) randomClass.newInstance();
if (t.getStatePointer() != null) {
// TODO: attach this thing to deallocator
// if it's stateless random - we just don't care then
}
t.setSeed(seed);
return t;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations