List of usage examples for java.util Random nextDouble
public double nextDouble()
From source file:org.apache.marmotta.kiwi.test.PersistenceTest.java
/** * Test storing and loading string literals (with type and without language). * * @throws SQLException/*from ww w. ja va 2 s. c o m*/ */ @Test public void testStoreDoubleLiteral() throws SQLException { KiWiConnection connection = persistence.getConnection(); try { KiWiUriResource uri = new KiWiUriResource(Namespaces.NS_XSD + "double"); Random rnd = new Random(); double value = rnd.nextDouble(); // add a new URI to the triple store and check if it exists afterwards, before and after commit KiWiDoubleLiteral literal = new KiWiDoubleLiteral(value, uri); connection.storeNode(literal); // check if it then has a database ID Assert.assertTrue(literal.getId() >= 0); KiWiNode testLiteral1 = connection.loadNodeById(literal.getId()); // needs to be equal, and should also be the identical object! Assert.assertEquals(literal, testLiteral1); Assert.assertEquals(uri, ((KiWiLiteral) testLiteral1).getType()); //Assert.assertTrue(literal == testLiteral1); connection.commit(); KiWiNode testLiteral2 = connection.loadNodeById(literal.getId()); // needs to be equal, and should also be the identical object! Assert.assertEquals(literal, testLiteral2); Assert.assertEquals(uri, ((KiWiLiteral) testLiteral2).getType()); //Assert.assertTrue(literal == testLiteral2); KiWiNode testLiteral3 = connection.loadLiteral(literal.stringValue(), null, uri); // needs to be equal, and should also be the identical object! Assert.assertEquals(literal, testLiteral3); Assert.assertEquals(uri, ((KiWiLiteral) testLiteral3).getType()); //Assert.assertTrue(literal == testLiteral3); // load by integer value KiWiNode testLiteral6 = connection.loadLiteral(value); // needs to be equal, and should also be the identical object! Assert.assertEquals(literal, testLiteral6); Assert.assertEquals(uri, ((KiWiLiteral) testLiteral6).getType()); //Assert.assertTrue(literal == testLiteral6); connection.commit(); // clear cache and test again persistence.clearCache(); KiWiNode testLiteral4 = connection.loadNodeById(literal.getId()); // needs to be equal, but now it should not be the same object! Assert.assertEquals(literal, testLiteral4); Assert.assertEquals(uri, ((KiWiLiteral) testLiteral4).getType()); //Assert.assertTrue(literal != testLiteral4); KiWiNode testLiteral5 = connection.loadLiteral(literal.stringValue(), null, uri); // needs to be equal, but now it should not be the same object! Assert.assertEquals(literal, testLiteral5); Assert.assertEquals(uri, ((KiWiLiteral) testLiteral5).getType()); //Assert.assertTrue(literal != testLiteral5); // load by integer value KiWiNode testLiteral7 = connection.loadLiteral(value); // needs to be equal, and should also be the identical object! Assert.assertEquals(literal, testLiteral7); Assert.assertEquals(uri, ((KiWiLiteral) testLiteral7).getType()); //Assert.assertTrue(literal != testLiteral7); connection.commit(); // finally do a test on the nodes table, it should contain exactly one entry PreparedStatement checkNodeStmt = connection.getJDBCConnection() .prepareStatement("SELECT * FROM nodes WHERE ntype='double'"); ResultSet result = checkNodeStmt.executeQuery(); Assert.assertTrue(result.next()); Assert.assertEquals((long) literal.getId(), result.getLong("id")); Assert.assertEquals(literal.stringValue(), result.getString("svalue")); Assert.assertEquals(value, result.getDouble("dvalue"), 0.01); Assert.assertEquals("double", result.getString("ntype")); Assert.assertNull(result.getString("lang")); Assert.assertEquals((long) uri.getId(), result.getLong("ltype")); result.close(); connection.commit(); } finally { connection.close(); } }
From source file:ml.shifu.shifu.core.dtrain.nn.NNMaster.java
private HashSet<Integer> dropoutNodes() { Random random = new Random(System.currentTimeMillis()); HashSet<Integer> droppedNodeIndices = new HashSet<Integer>(); // from input to last hidden layer. (exclude output layer) for (int i = this.flatNetwork.getLayerIndex().length - 1; i > 0; i--) { int beginNeuronIndex = this.flatNetwork.getLayerIndex()[i]; // exclude constant neuron int neuronCount = this.flatNetwork.getLayerFeedCounts()[i]; // from first neuron to last neuron in current layer for (int j = 0; j < neuronCount; j++) { if (random.nextDouble() < this.flatNetwork.getLayerDropoutRates()[i]) { // drop this node by adding it into list and will passing // this list to workers droppedNodeIndices.add(beginNeuronIndex + j); }//w w w . j a v a2 s. c om } } LOG.info("layerIndex:{}; layerCounts:{}; dropoutNodes:{}", Arrays.toString(this.flatNetwork.getLayerIndex()), Arrays.toString(this.flatNetwork.getLayerCounts()), Arrays.toString(droppedNodeIndices.toArray(new Integer[droppedNodeIndices.size()]))); return droppedNodeIndices; }
From source file:org.elasticsearch.test.ElasticsearchIntegrationTest.java
private static Settings.Builder setRandomMerge(Random random, Settings.Builder builder) { if (random.nextBoolean()) { builder.put(MergePolicyConfig.INDEX_COMPOUND_FORMAT, random.nextBoolean() ? random.nextDouble() : random.nextBoolean()); }// ww w. ja v a2s . co m switch (random.nextInt(4)) { case 3: final int maxThreadCount = RandomInts.randomIntBetween(random, 1, 4); final int maxMergeCount = RandomInts.randomIntBetween(random, maxThreadCount, maxThreadCount + 4); builder.put(MergeSchedulerConfig.MAX_MERGE_COUNT, maxMergeCount); builder.put(MergeSchedulerConfig.MAX_THREAD_COUNT, maxThreadCount); break; } return builder; }
From source file:br.fapesp.myutils.MyUtils.java
/** * Selecting a value from values array at random using the weights provided * @param values//from w ww . j av a 2 s . c o m * @param weights MUST sum to 1.0 * @param rng * @param seed * @return */ public static int weightedValueRandomSelection(ArrayList<Integer> values, double[] weights, Random rng, long seed) { if (Math.abs(MyUtils.sum(weights) - 1.0) > 0.01) throw new RuntimeException("weights must sum to 1.0!"); if (values.size() != weights.length) throw new RuntimeException("values and weights must be of the same size!"); Random r; if (rng == null) r = new Random(seed); else r = rng; double[] _weights = new double[weights.length]; int[] sortidx = new int[weights.length]; double flip; System.arraycopy(weights, 0, _weights, 0, weights.length); Arrays.sort(_weights); ArrayList<Integer> _values = new ArrayList<Integer>(); // find out the indexes of the correct sorting: for (int i = 0; i < weights.length; i++) { for (int j = 0; j < weights.length; j++) if (_weights[i] == weights[j]) { sortidx[i] = j; break; } } // add the values in the sort order: for (int i = 0; i < weights.length; i++) _values.add(values.get(sortidx[i])); flip = r.nextDouble(); // now check from the largest probability to the lowest, which one was selected: double cumsum = 0; for (int i = weights.length - 1; i >= 0; i--) { cumsum += _weights[i]; if (flip <= cumsum) return _values.get(i); } throw new RuntimeException("error occurred"); }
From source file:qtiscoringengine.ExprNot.java
@Override protected DataElement exprEvaluate(VariableBindings vb, QTIRubric rubric, List<DataElement> paramValues) { _DEFloat min = (_DEFloat) getAttributeValue("min"); _DEFloat max = (_DEFloat) getAttributeValue("max"); Random randomizer = new Random(); double rand = randomizer.nextDouble(); return new DEFloat(min.getValue().doubleValue() + rand * (max.getValue().doubleValue() - min.getValue().doubleValue())); }
From source file:qtiscoringengine.ExprNot.java
@Override protected DataElement exprEvaluate(VariableBindings vb, QTIRubric rubric, List<DataElement> paramValues) { DEInteger minx = (DEInteger) getAttributeValue("min"); DEInteger maxx = (DEInteger) getAttributeValue("max"); DEInteger stepx = (DEInteger) getAttributeValue("step"); int min = (minx == null) ? 0 : minx.getValue(); int step = (stepx == null) ? 0 : stepx.getValue(); int max = maxx.getValue(); int valCount = (max - min) / step; Random randomizer = new Random(); double rand = randomizer.nextDouble() * 1000000; int val = (int) (rand % valCount); return new DEInteger((int) val * step + min); }
From source file:com.ibm.bi.dml.test.utils.TestUtils.java
/** * <p>/*from w ww. j ava 2 s.com*/ * Returns a random double value. * </p> * * @return random double value */ public static double getRandomDouble() { Random random = new Random(System.currentTimeMillis()); double randomValue = random.nextInt() * random.nextDouble(); return randomValue; }
From source file:qtiscoringengine.ExprNot.java
@Override protected DataElement exprEvaluate(VariableBindings vb, QTIRubric rubric, List<DataElement> paramValues) { Random randomizer = new Random(); int randSet = (int) MathUtils.truncate(randomizer.nextDouble() * 1000000); DEContainer param = (DEContainer) paramValues.get(0); if (param == null) return null; if (param.getMemberCount() == 0) return null; int idx = randSet % param.getMemberCount(); return param.getMember(idx); }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
public static int[] selectEqualProbability(int[] id) { // select item based on equal probability for all elements int[] results = new int[2]; double step = 1 / (id.length); Random rand = new Random(); double sel = rand.nextDouble(); double prob = 0; for (int i = 0; i < id.length; i++) { prob = prob + step;//w ww . j a v a2 s.com if (prob > sel) { results[0] = id[i]; results[1] = i; return results; } } results[0] = id[id.length - 1]; results[1] = id.length - 1; return results; }
From source file:de.tum.bgu.msm.syntheticPopulationGenerator.kagawa.SyntheticPopJP.java
public static int[] selectEqualProbability(int[] id, int length) { // select item based on equal probability for all elements, given a constraint length (only consider until one row, not the complete array) int[] results = new int[2]; double step = 1 / (length); Random rand = new Random(); double sel = rand.nextDouble(); double prob = 0; for (int i = 0; i < length; i++) { prob = prob + step;/* ww w . java 2 s.co m*/ if (prob > sel) { results[0] = id[i]; results[1] = i; return results; } } results[0] = id[length - 1]; results[1] = length - 1; return results; }