Example usage for java.util Random nextFloat

List of usage examples for java.util Random nextFloat

Introduction

In this page you can find the example usage for java.util Random nextFloat.

Prototype

public float nextFloat() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random number generator's sequence.

Usage

From source file:Main.java

public static float ramdomMinMaxFloat(float min, float max) {

    Random random = new Random();

    float rs = -1;

    while (rs < min || rs > max) {

        rs = random.nextFloat();
    }/*  w w w . ja  v  a2s . com*/

    return rs;
}

From source file:Main.java

public static void initialize_weight(float[] weight, int size, boolean randomFlag) {
    // if randomFlag == true, generate weight randomly
    // otherwise, initialize all weight to 0.0
    Random randomGenerator = new Random();
    for (int i = 0; i < size; i++) {
        if (randomFlag == true) {
            weight[i] = randomGenerator.nextFloat();
        } else {//from   www.j  a  va 2s.c  om
            weight[i] = (float) 0.0;
        }
    }
}

From source file:Main.java

public static void genRandom(long seed, int factor, int offset, float array[]) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length; i++) {
        array[i] = r.nextFloat() * factor + offset;
    }//from   w  w  w .j a va 2s  .c  o m
}

From source file:iddb.core.util.PasswordUtils.java

public static String hashPassword(String raw_password) {
    Random random = new Random(raw_password.length() + System.currentTimeMillis());
    String salt = StringUtils.left(HashUtils.getSHA1Hash(
            Float.toHexString(random.nextFloat()) + Float.toHexString(System.currentTimeMillis())), 5);
    String hashedPassword = HashUtils.getSHA1Hash(salt + raw_password);
    return salt + "$" + hashedPassword;
}

From source file:org.blockartistry.DynSurround.client.fx.particle.ExplosionHelper.java

private static ParticleAsset getParticle(@Nonnull final World world, final double x, final double y,
        final double z) {
    final Random rand = XorShiftRandom.current();
    final Assets assets = getAssets(world);

    final float motionX = rand.nextFloat() * 10.0F - 5.0F;
    final float motionZ = rand.nextFloat() * 10.0F - 5.0F;
    final float motionY = rand.nextFloat() * 6.0F + 6.0F;

    final int choice = rand.nextInt(20);
    if (choice < 3 && ModOptions.addMobParticles) {
        final String mob = assets.getMob(rand);
        if (StringUtils.isEmpty(mob))
            return null;
        final ParticleEntity pe = new ParticleEntity(mob, world, x, y, z, motionX, motionY, motionZ);
        pe.setScale(1.0F);/*from  w w w.  j a v a 2 s  . com*/
        pe.setMaxAge(75);
        pe.setPitchRate(18 + rand.nextFloat() * 18);
        pe.setYawRate(18 + rand.nextFloat() * 18);
        pe.setGravity(0.25F);
        return pe;
    } else if (choice < 8) {
        final ItemStack stack = assets.getStack(rand);
        if (stack == null)
            return null;
        final ParticleItemStack s = new ParticleItemStack(stack, world, x, y, z, motionX, motionY, motionZ);
        s.setScale(0.5F);
        s.setMaxAge(75);
        s.setPitchRate(18 + rand.nextFloat() * 18);
        s.setYawRate(18 + rand.nextFloat() * 18);
        s.setGravity(0.25F);
        return s;
    } else {
        final Block block = assets.getBlock(rand);
        if (block == null)
            return null;
        final ParticleBlock p = new ParticleBlock(block, world, x, y, z, motionX, motionY, motionZ);
        p.setScale(0.05F + 0.10F * rand.nextFloat());
        p.setMaxAge(75);
        p.setPitchRate(18 + rand.nextFloat() * 18);
        p.setYawRate(18 + rand.nextFloat() * 18);
        p.setGravity(0.25F);
        return p;
    }

}

From source file:Main.java

public static void genRandom(long seed, int factor, int offset, float array[], int stride, int skip) {
    Random r = new Random(seed);
    for (int i = 0; i < array.length / stride; i++) {
        for (int j = 0; j < stride; j++) {
            if (j >= stride - skip)
                array[i * stride + j] = 0;
            else/* www .j  av a  2  s .  c o  m*/
                array[i * stride + j] = r.nextFloat() * factor + offset;
        }
    }
}

From source file:com.linkedin.pinot.core.data.readers.PinotSegmentUtil.java

private static Object generateSingleValue(Random random, FieldSpec.DataType dataType) {
    switch (dataType) {
    case INT://from w w w  . j  a v  a2 s.  c  o  m
        return Math.abs(random.nextInt());
    case LONG:
        return Math.abs(random.nextLong());
    case FLOAT:
        return Math.abs(random.nextFloat());
    case DOUBLE:
        return Math.abs(random.nextDouble());
    case STRING:
        return RandomStringUtils.randomAlphabetic(DEFAULT_STRING_VALUE_LENGTH);
    default:
        throw new IllegalStateException("Illegal data type");
    }
}

From source file:org.apache.helix.taskexecution.TaskExecutionDemo.java

private static void populateDummyData(TaskResultStore taskResultStore) throws Exception {
    float fraudProbability = 0.01f;
    float clickProbability = 0.01f;
    int numImps = NUM_IMP_EVENTS;
    Random rand = new Random();
    String[] countries = { "US", "CANADA", "UK", "CHINA", "UNKNOWN" };
    String[] genders = { "M", "F", "UNKNOWN" };
    for (int i = 0; i < numImps; i++) {
        boolean isFraudulent = (rand.nextFloat() <= fraudProbability);
        String impEventId = "" + Math.abs(rand.nextLong());
        String impEvent = impEventId; // event id
        impEvent += "," + isFraudulent;
        impEvent += "," + countries[rand.nextInt(countries.length)];
        impEvent += "," + genders[rand.nextInt(genders.length)];
        taskResultStore.rpush(FilterTask.IMPRESSIONS, impEvent);

        boolean isClick = (rand.nextFloat() <= clickProbability);
        if (isClick) {
            String clickEvent = "" + Math.abs(rand.nextLong()); // event id
            isFraudulent = (rand.nextFloat() <= fraudProbability);
            clickEvent += "," + isFraudulent;
            clickEvent += "," + impEventId;
            taskResultStore.rpush(FilterTask.CLICKS, clickEvent);
        }/*from w w  w .  ja v  a2 s  .  c o  m*/
    }
    System.out.println("Done populating dummy data");
}

From source file:org.silverpeas.authentication.encryption.UnixSHA512Encryption.java

private static final String computeRandomSalt() {
    java.util.Random random = new java.util.Random();
    StringBuilder saltBuf = new StringBuilder(ENCRYPTION_METHOD_ID);

    while (saltBuf.length() < 16) {
        int index = (int) (random.nextFloat() * SALTCHARS.length());
        saltBuf.append(SALTCHARS.substring(index, index + 1));
    }/*w w  w  .jav a2  s.  c  o  m*/

    return saltBuf.toString();
}

From source file:com.abhinavjhanwar.android.egg.neko.Cat.java

private static float frandrange(Random r) {
    float a = 0.5f;
    float b = 1f;
    return (b - a) * r.nextFloat() + a;
}