Example usage for java.util Random nextBoolean

List of usage examples for java.util Random nextBoolean

Introduction

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

Prototype

public boolean nextBoolean() 

Source Link

Document

Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's sequence.

Usage

From source file:Test.java

private static void startUpdatingThread(final List<String> list) {
    updatingThread = new Thread(new Runnable() {
        long counter = 0;

        public void run() {
            while (!Thread.interrupted()) {
                int size = list.size();
                Random random = new Random();
                if (random.nextBoolean()) {
                    if (size > 1) {
                        list.remove(random.nextInt(size - 1));
                    }//from   w ww  .  j a  va 2s  .  co  m
                } else {
                    if (size < 20) {
                        list.add("Random string " + counter);
                    }
                }
                counter++;
            }
        }

    });
    updatingThread.start();
}

From source file:Main.java

public static boolean[] generateRandomBooleanArray(int size) {
    boolean[] res = new boolean[size];
    Random r = new Random();
    for (int i = 0; i < size; i++)
        res[i] = r.nextBoolean();
    return res;/*  w  ww  .j a  v a2s .  c  o  m*/
}

From source file:com.g414.dgen.field.Fields.java

/** Returns a random boolean */
public static Field<Boolean> getBooleanField(final String name) {
    return new Field<Boolean>() {
        @Override/*from  w w w  . j  a  v a2  s. c  o  m*/
        public String getName() {
            return name;
        }

        @Override
        public Boolean getValue(String entityId, Map<String, Object> entitySoFar, Random random) {
            return random.nextBoolean();
        }
    };
}

From source file:org.apache.hadoop.hdfs.protocol.RandomObjectsGenerators.java

static LocatedBlocksWithMetaInfo rndLocatedBlocksWithMetaInfo(Random rnd) {
    return new LocatedBlocksWithMetaInfo(rnd.nextInt(), Arrays.asList(rndLocatedBlockArr(rnd, 13)),
            rnd.nextBoolean(), rnd.nextInt(), rnd.nextInt(), rnd.nextInt());
}

From source file:com.gatf.executor.core.GatfFunctionHandler.java

public static String handleFunction(String function) {
    if (function.equals(BOOLEAN)) {
        Random rand = new Random();
        return String.valueOf(rand.nextBoolean());
    } else if (function.matches(DT_FMT_REGEX)) {
        Matcher match = datePattern.matcher(function);
        match.matches();//from  w ww .j  av a  2 s . c o m
        SimpleDateFormat format = new SimpleDateFormat(match.group(1));
        return format.format(new Date());
    } else if (function.matches(DT_FUNC_FMT_REGEX)) {
        Matcher match = specialDatePattern.matcher(function);
        match.matches();
        String formatStr = match.group(1);
        String operation = match.group(2);
        int value = Integer.valueOf(match.group(3));
        String unit = match.group(4);
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            Date dt = format.parse(format.format(new Date()));
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);

            value = (operation.equals(MINUS) ? -value : value);
            if (unit.equals(YEAR)) {
                cal.add(Calendar.YEAR, value);
            } else if (unit.equals(MONTH)) {
                cal.add(Calendar.MONTH, value);
            } else if (unit.equals(DAY)) {
                cal.add(Calendar.DAY_OF_YEAR, value);
            } else if (unit.equals(HOUR)) {
                cal.add(Calendar.HOUR_OF_DAY, value);
            } else if (unit.equals(MINUITE)) {
                cal.add(Calendar.MINUTE, value);
            } else if (unit.equals(SECOND)) {
                cal.add(Calendar.SECOND, value);
            } else if (unit.equals(MILLISECOND)) {
                cal.add(Calendar.MILLISECOND, value);
            }
            return format.format(cal.getTime());
        } catch (Exception e) {
            throw new AssertionError("Invalid date format specified - " + formatStr);
        }
    } else if (function.equals(FLOAT)) {
        Random rand = new Random(12345678L);
        return String.valueOf(rand.nextFloat());
    } else if (function.equals(ALPHA)) {
        return RandomStringUtils.randomAlphabetic(10);
    } else if (function.equals(ALPHANUM)) {
        return RandomStringUtils.randomAlphanumeric(10);
    } else if (function.equals(NUMBER_PLUS)) {
        Random rand = new Random();
        return String.valueOf(rand.nextInt(1234567));
    } else if (function.equals(NUMBER_MINUS)) {
        Random rand = new Random();
        return String.valueOf(-rand.nextInt(1234567));
    } else if (function.equals(NUMBER)) {
        Random rand = new Random();
        boolean bool = rand.nextBoolean();
        return bool ? String.valueOf(rand.nextInt(1234567)) : String.valueOf(-rand.nextInt(1234567));
    } else if (function.matches(RANDOM_RANGE_REGEX)) {
        Matcher match = randRangeNum.matcher(function);
        match.matches();
        String min = match.group(1);
        String max = match.group(2);
        try {
            int nmin = Integer.parseInt(min);
            int nmax = Integer.parseInt(max);
            return String.valueOf(nmin + (int) (Math.random() * ((nmax - nmin) + 1)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.streamsets.pipeline.stage.util.http.HttpStageTestUtil.java

@NotNull
public static String randomizeCapitalization(Random random, String input) {
    StringBuilder header = new StringBuilder();
    for (int i = 0; i < input.length(); i++) {
        String nextChar = input.substring(i, i + 1);
        if (random.nextBoolean()) {
            header.append(nextChar.toUpperCase());
        } else {/*w  w w  .j  a  v a 2  s .  c o  m*/
            header.append(nextChar.toLowerCase());
        }
    }
    return header.toString();
}

From source file:org.wso2.carbon.sample.performance.Client.java

private static void sendWarmUpEvents(DataPublisher dataPublisher, long warmUpCount) {
    long counter = 0;
    Random randomGenerator = new Random();
    String streamId = "org.wso2.event.sensor.stream:1.0.0";

    while (counter < warmUpCount) {
        boolean isPowerSaveEnabled = randomGenerator.nextBoolean();
        int sensorId = randomGenerator.nextInt();
        double longitude = randomGenerator.nextDouble();
        double latitude = randomGenerator.nextDouble();
        float humidity = randomGenerator.nextFloat();
        double sensorValue = randomGenerator.nextDouble();
        Event event = new Event(streamId, System.currentTimeMillis(),
                new Object[] { System.currentTimeMillis(), isPowerSaveEnabled, sensorId, "warmup-" + counter },
                new Object[] { longitude, latitude }, new Object[] { humidity, sensorValue });

        dataPublisher.publish(event);/*from   w  w  w  .  j a  va  2  s. co m*/
        counter++;
    }
}

From source file:org.apache.hadoop.hbase.regionserver.TestCacheOnWriteInSchema.java

private static KeyValue.Type generateKeyType(Random rand) {
    if (rand.nextBoolean()) {
        // Let's make half of KVs puts.
        return KeyValue.Type.Put;
    } else {/* ww  w.j  a  v  a  2  s  . c o  m*/
        KeyValue.Type keyType = KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
        if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) {
            throw new RuntimeException("Generated an invalid key type: " + keyType + ". "
                    + "Probably the layout of KeyValue.Type has changed.");
        }
        return keyType;
    }
}

From source file:org.apache.hadoop.hdfs.protocol.RandomObjectsGenerators.java

static HdfsFileStatus[] rndHdfsFileStatusArr(Random rnd, int count) {
    HdfsFileStatus[] files = new HdfsFileStatus[count];
    for (int i = 0; i < files.length; i++) {
        files[i] = new HdfsFileStatus(rnd.nextLong(), rnd.nextBoolean(), rnd.nextInt(5), rnd.nextLong(),
                rnd.nextLong(), rnd.nextLong(), new FsPermission((short) rnd.nextInt()),
                "owner" + rnd.nextInt(), "group" + rnd.nextInt(), rndByteArr(rnd, rnd.nextInt(37)));
    }//www  . ja v  a  2  s  .  c  o  m
    return files;
}

From source file:net.duckling.ddl.web.api.rest.TeamController.java

private static String getRandomString(Integer length) {
    String str = "";
    Random random = new Random();
    for (int i = 0; i < length; i++) {
        boolean b = random.nextBoolean();
        if (b) {//  ww  w  .  j a  va2 s .c o  m
            str += (char) (97 + random.nextInt(26));// ???  
        } else {
            str += String.valueOf(random.nextInt(10));
        }
    }
    return str;
}