List of usage examples for java.util Random nextFloat
public float nextFloat()
From source file:services.SecurityService.java
public String getOtp() { String SALTCHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; StringBuilder salt = new StringBuilder(); Random rnd = new Random(); while (salt.length() < 6) { // length of the random string. int index = (int) (rnd.nextFloat() * SALTCHARS.length()); salt.append(SALTCHARS.charAt(index)); }// w w w .ja v a2 s.co m String saltStr = salt.toString(); return saltStr; }
From source file:com.ms.commons.lang.RangeBuilderTest.java
@Test public void testPrimitive() { List<Person> data = new ArrayList<Person>(); for (int i = 0; i < 5; i++) { Random r = new Random(System.nanoTime()); Person p = new Person(); p.setSalary(r.nextFloat()); p.setAge((r.nextInt(10)));/*from ww w . j ava2s.c om*/ p.setId(r.nextLong()); p.setSex(r.nextInt(10)); p.setName("name" + r.nextInt(10)); data.add(p); } println(data); Range range = RangeBuilder.data(data).property("age").range(); println(range); range = RangeBuilder.data(data).property("name").keyName("newName").range(); println(range); range = RangeBuilder.data(data).property("salary").desc().range(); println(range); range = RangeBuilder.data(data).property("id").asc().range(); println(range); }
From source file:org.apache.solr.cloud.TestRandomFlRTGCloud.java
/** * Randomly chooses to do a commit, where the probability of doing so increases the longer it's been since * a commit was done./*w w w . j a v a2 s .c o m*/ * * @returns <code>0</code> if a commit was done, else <code>itersSinceLastCommit + 1</code> */ private static int maybeCommit(final Random rand, final int itersSinceLastCommit, final int numIters) throws IOException, SolrServerException { final float threshold = itersSinceLastCommit / numIters; if (rand.nextFloat() < threshold) { log.info("COMMIT"); assertEquals(0, getRandClient(rand).commit().getStatus()); return 0; } return itersSinceLastCommit + 1; }
From source file:org.diorite.commons.math.DioriteRandomUtils.java
public static float getRandomFloat(Random random, float min, float max) throws IllegalArgumentException { if (Float.compare(min, max) == 0) { return max; }// w ww .j a v a 2 s .co m Validate.isTrue(max > min, "Max can't be smaller than min!"); return (random.nextFloat() * (max - min)) + min; }
From source file:org.apache.edgent.samples.utils.sensor.PeriodicRandomSensor.java
/** * Create a periodic sensor stream with readings from {@link Random#nextFloat()}. * @param t the topology to add the sensor stream to * @param periodMsec how frequently to generate a reading * @return the sensor value stream/*from www. j a v a2 s.co m*/ */ public TStream<Pair<Long, Float>> newFloat(Topology t, long periodMsec) { Random r = newRandom(); return t.poll(() -> new Pair<Long, Float>(System.currentTimeMillis(), r.nextFloat()), periodMsec, TimeUnit.MILLISECONDS); }
From source file:se.llbit.chunky.world.entity.PlayerEntity.java
private void randomLook(Random random) { yaw = (random.nextFloat() - 0.5) * QuickMath.TAU; headYaw = 0.4 * (random.nextFloat() - 0.5) * QuickMath.HALF_PI; pitch = (random.nextFloat() - 0.5) * QuickMath.HALF_PI; }
From source file:de.brendamour.jpasskit.server.PKRestletServerResourceFactory.java
public PKPassResource getPKPassResource() { return new PKPassResource("passes/coupons.raw") { @Override/*ww w. j a va 2s .co m*/ protected GetPKPassResponse handleGetLatestVersionOfPass(final String passTypeIdentifier, final String serialNumber, final String authString, final Date modifiedSince) throws PKAuthTokenNotValidException, PKPassNotModifiedException { PKPass pass = new PKPass(); try { pass = jsonObjectMapper.readValue(new File("passes/coupons.raw/pass2.json"), PKPass.class); float newAmount = getNewRandomAmount(); PKStoreCard storeCard = pass.getStoreCard(); List<PKField> primaryFields = storeCard.getPrimaryFields(); for (PKField field : primaryFields) { if ("balance".equals(field.getKey())) { field.setValue(newAmount); field.setChangeMessage("Amount changed to %@"); break; } } List<PKField> headerFields = storeCard.getHeaderFields(); for (PKField field : headerFields) { if ("balanceHeader".equals(field.getKey())) { field.setValue(newAmount); break; } } } catch (Exception e) { throw new RuntimeException(e); } GetPKPassResponse getPKPassResponse = new GetPKPassResponse(pass, new Date()); return getPKPassResponse; } private float getNewRandomAmount() { Random random = new Random(); float amount = random.nextInt(100) + random.nextFloat(); BigDecimal bigDecimalForRounding = new BigDecimal(amount).setScale(2, RoundingMode.HALF_EVEN); return bigDecimalForRounding.floatValue(); } @Override protected PKSigningInformation getSingingInformation() { try { return PKSigningUtil.loadSigningInformationFromPKCS12FileAndIntermediateCertificateFile( PKCS12_FILE_PATH, PKCS12_FILE_PASSWORD, APPLE_WWDRCA_CERT_PATH); } catch (Exception e) { throw new RuntimeException(e); } } }; }
From source file:se.llbit.chunky.world.entity.PlayerEntity.java
private void randomPose(Random random) { leftLegPose = (random.nextFloat() - 0.5) * QuickMath.HALF_PI; rightLegPose = -leftLegPose;//from w w w .ja v a 2 s . co m leftArmPose = (random.nextFloat() - 0.5) * QuickMath.HALF_PI; rightArmPose = -leftArmPose; }
From source file:appeng.block.misc.BlockCharger.java
@Override @SideOnly(Side.CLIENT)/*from w w w .ja v a 2 s . c o m*/ public void randomDisplayTick(final IBlockState state, final World w, final BlockPos pos, final Random r) { if (!AEConfig.instance().isEnableEffects()) { return; } if (r.nextFloat() < 0.98) { return; } final AEBaseTile tile = this.getTileEntity(w, pos); if (tile instanceof TileCharger) { final TileCharger tc = (TileCharger) tile; if (AEApi.instance().definitions().materials().certusQuartzCrystalCharged() .isSameAs(tc.getStackInSlot(0))) { final double xOff = 0.0; final double yOff = 0.0; final double zOff = 0.0; for (int bolts = 0; bolts < 3; bolts++) { if (CommonHelper.proxy.shouldAddParticles(r)) { final LightningFX fx = new LightningFX(w, xOff + 0.5 + pos.getX(), yOff + 0.5 + pos.getY(), zOff + 0.5 + pos.getZ(), 0.0D, 0.0D, 0.0D); Minecraft.getMinecraft().effectRenderer.addEffect(fx); } } } } }
From source file:br.unicamp.cst.behavior.glas.ActionSelectionCodelet.java
@Override public void proc() { if (enabled) { //Update Solution Tree if needed Object new_solution_tree_string = SOLUTION_TREE_MO.getI(); if (!new_solution_tree_string.equals(this.current_solution_tree_jsonarray.toString())) { // System.out.println("Action Selection Found a new Solution Tree: "+new_solution_tree_string); JSONArray new_solution_tree_jsonarray; try { new_solution_tree_jsonarray = new JSONArray(new_solution_tree_string); int[] new_solution_tree_phenotype = new int[new_solution_tree_jsonarray.length()]; for (int i = 0; i < new_solution_tree_phenotype.length; i++) { new_solution_tree_phenotype[i] = new_solution_tree_jsonarray.getInt(i); }/* www . ja v a 2s . c om*/ current_solution_tree_jsonarray = new_solution_tree_jsonarray; sm = new GlasActionSelection(new_solution_tree_phenotype); } catch (JSONException e) { e.printStackTrace(); } solution_tree_fitness = SOLUTION_TREE_MO.getEvaluation(); // exp_factor=1-(1/(1+Math.exp(-solution_tree_fitness))); //1/(1+EXP(AB3*10)) if (dynamicExplorationOn) { exp_factor = (1 / (1 + Math.exp(10 * solution_tree_fitness))); } } //Selects action based on stimulus boolean new_stim = (NEW_STIM_MO.getI().equals(String.valueOf(true))); boolean new_action = (NEW_ACTION_MO.getI().equals(String.valueOf(true))); boolean new_reward = (NEW_REWARD_MO.getI().equals(String.valueOf(true))); if (!STIMULUS_MO.getI().equals("") && new_stim && !new_action && !new_reward) { int[] stimulus = { Integer.valueOf((String) STIMULUS_MO.getI()) }; int[] selected_action = sm.runStimuli(stimulus); //TODO Ugly solution //TODO Add an exploratory element here? Random rnd_exp = new Random(); if (rnd_exp.nextFloat() <= exp_factor) { int[] actions = sm.getActions(); selected_action[0] = actions[rnd_exp.nextInt(actions.length)]; } ACTION_MO.updateI(Integer.toString(selected_action[0])); //TODO is [0] correct? // System.out.println("ACTION_MO.updateInfo("+selected_action[0]+")"); new_action = true; // System.out.println("new_action=true;"); } NEW_ACTION_MO.updateI(String.valueOf(new_action)); } //end if(enabled) }