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:alluxio.client.rest.TestCaseOptionsTest.java

/**
 * Tests getting and setting fields./*  w  ww .  j av a2 s .c  o  m*/
 */
@Test
public void fields() {
    Random random = new Random();
    Object body = new Object();
    byte[] bytes = new byte[5];
    random.nextBytes(bytes);
    InputStream inputStream = new ByteArrayInputStream(bytes);
    boolean prettyPrint = random.nextBoolean();
    TestCaseOptions options = TestCaseOptions.defaults();
    String md5 = RandomStringUtils.random(64);

    options.setBody(body);
    options.setInputStream(inputStream);
    options.setPrettyPrint(prettyPrint);
    options.setMD5(md5);

    Assert.assertEquals(body, options.getBody());
    Assert.assertEquals(inputStream, options.getInputStream());
    Assert.assertEquals(prettyPrint, options.isPrettyPrint());
    Assert.assertEquals(md5, options.getMD5());
}

From source file:org.sakaiproject.assignment.impl.AssignmentServiceTest.java

/**
 * /*from   w w w  . j a  v  a2  s.co m*/
 * test the zip routine without/with flushing for 5 times
 *
 */
public void testZipSubmissionsWithoutFlushing() {
    if (testZipFunction) {
        System.out.println("student number = " + userNumber);
        System.out.println("attachment size = " + attachmentSize + "KB");
        System.gc();

        for (int index = 1; index <= testNumber; index++) {
            Random ran = new Random();
            boolean flushing = ran.nextBoolean();

            Runtime r = Runtime.getRuntime();
            System.out.println("with flushing = " + flushing);
            long mBefore = r.freeMemory();
            long tBefore = System.currentTimeMillis();

            zipWithFlushing(flushing);
            long mAfter = r.freeMemory();
            long tAfter = System.currentTimeMillis();
            System.out.println("free memory before invoke " + mBefore + " after " + mAfter);
            System.out.println("minute time used " + (tAfter - tBefore) / (1000.0 * 60.0));
            System.out.println("percent  " + (mBefore - mAfter) * 100 / (mBefore * 1.0));
            System.out.println("*************");
            // gc
            System.gc();
        }
    }
}

From source file:com.netflix.curator.framework.recipes.locks.TestChildReaper.java

@Test
public void testSomeNodes() throws Exception {

    Timing timing = new Timing();
    ChildReaper reaper = null;//from w  w  w. j a  va  2s  . c o  m
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(),
            timing.connection(), new RetryOneTime(1));
    try {
        client.start();

        Random r = new Random();
        int nonEmptyNodes = 0;
        for (int i = 0; i < 10; ++i) {
            client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
            if (r.nextBoolean()) {
                client.create().forPath("/test/" + Integer.toString(i) + "/foo");
                ++nonEmptyNodes;
            }
        }

        reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
        reaper.start();

        timing.forWaiting().sleepABit();

        Stat stat = client.checkExists().forPath("/test");
        Assert.assertEquals(stat.getNumChildren(), nonEmptyNodes);
    } finally {
        IOUtils.closeQuietly(reaper);
        IOUtils.closeQuietly(client);
    }
}

From source file:com.somethoughts.chinmay.game.Coin.CoinTossMainFragment.java

private void toss_it(final Boolean userChoice) {

    if (inProgress) {
        Toast.makeText(getActivity(), "Please Wait", Toast.LENGTH_SHORT).show();
        return;/*from  w  w w . j av  a 2s .c om*/
    }
    final TextView textViewResult = (TextView) view.findViewById(R.id.coin_result_textview);
    final TextView textViewStatus = (TextView) view.findViewById(R.id.coin_status_textView);
    final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.coin_progressBar);

    Random random = new Random();
    progressBar.setVisibility(View.VISIBLE);
    progressBar.setMax(2500);
    progressBar.setIndeterminate(false);
    final Boolean b = random.nextBoolean();
    countDownTimer = new CountDownTimer(3000, 125) {
        @Override
        public void onTick(long l) {
            inProgress = true;
            Random random = new Random();
            progressBar.setProgress(3000 - (int) l);
            Log.v("Progress", Integer.toString(progressBar.getProgress()));
            textViewStatus.setVisibility(View.VISIBLE);
            textViewStatus.setText(toss[random.nextInt(2)]);
            textViewResult.setText(getResources().getText(R.string.Waiting));
        }

        @Override
        public void onFinish() {
            inProgress = false;
            if ((b && userChoice) || (!b && !userChoice)) {

                textViewStatus.setText(getResources().getText(R.string.Voila));
                view.setBackgroundColor(getColor(getActivity().getBaseContext(), R.color.colorWin));
                if (userChoice)
                    textViewResult.setText(toss[0]);
                else
                    textViewResult.setText(toss[1]);
            } else {
                textViewStatus.setText(getResources().getText(R.string.oops));
                view.setBackgroundColor(getColor(getActivity().getBaseContext(), R.color.colorLose));
                if (userChoice)
                    textViewResult.setText(toss[1]);
                else
                    textViewResult.setText(toss[0]);
            }
            progressBar.setProgress(3000);
        }
    }.start();
}

From source file:uk.ac.imperial.presage2.core.simulator.DeclaredParameterTest.java

@Test
public void testTypes()
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    Random rnd = new Random();
    TestSource source = new TestSource();
    String[] names = { "testInt", "testDouble", "testBool", "testEnum", "testString" };
    Object[] values = { rnd.nextInt(), rnd.nextDouble(), rnd.nextBoolean(), EnumTest.values()[rnd.nextInt(3)],
            RandomStringUtils.randomAlphanumeric(rnd.nextInt(500)) };
    for (int i = 0; i < names.length; i++) {
        Field f = TestSource.class.getField(names[i]);
        Parameter a1 = f.getAnnotation(Parameter.class);
        DeclaredParameter test = new DeclaredParameter(a1, source, f);

        test.setValue(values[i].toString());
        assertEquals(values[i], f.get(source));
        assertEquals(names[i], test.name);
        assertFalse(test.optional);// ww w.j  a v a  2  s  .  co m
    }
}

From source file:org.etudes.mneme.impl.ShuffleTest.java

protected List<String> shuffle4(List<String> seq, int draw, int unique) {
    long seed = Integer.toString(unique).hashCode();
    Random rnd = new Random(seed);

    List<String> rv = new ArrayList<String>(seq);

    // pick a random to go forward or backward
    if (rnd.nextBoolean()) {
        for (int i = 0; i < seq.size(); i++) {
            // pick a random other position to swap with
            int x = rnd.nextInt(seq.size());
            if (x != i) {
                String hold = rv.get(x);
                rv.set(x, rv.get(i));/*  w  ww .j a  va  2  s  .co  m*/
                rv.set(i, hold);
            }
        }
    } else {
        for (int i = seq.size() - 1; i >= 0; i--) {
            // pick a random other position to swap with
            int x = rnd.nextInt(seq.size());
            if (x != i) {
                String hold = rv.get(x);
                rv.set(x, rv.get(i));
                rv.set(i, hold);
            }
        }
    }
    if (draw < rv.size()) {
        rv = rv.subList(0, draw);
    }
    return rv;
}

From source file:org.apache.hadoop.hdfs.server.blockmanagement.TestCachedBlocksList.java

private void testRemoveElementsFromList(Random r, CachedBlocksList list, CachedBlock[] blocks) {
    int i = 0;//www .j av a  2  s  .  c om
    for (Iterator<CachedBlock> iter = list.iterator(); iter.hasNext();) {
        Assert.assertEquals(blocks[i], iter.next());
        i++;
    }
    if (r.nextBoolean()) {
        LOG.info("Removing via iterator");
        for (Iterator<CachedBlock> iter = list.iterator(); iter.hasNext();) {
            iter.next();
            iter.remove();
        }
    } else {
        LOG.info("Removing in pseudo-random order");
        CachedBlock[] remainingBlocks = Arrays.copyOf(blocks, blocks.length);
        for (int removed = 0; removed < remainingBlocks.length;) {
            int toRemove = r.nextInt(remainingBlocks.length);
            if (remainingBlocks[toRemove] != null) {
                Assert.assertTrue(list.remove(remainingBlocks[toRemove]));
                remainingBlocks[toRemove] = null;
                removed++;
            }
        }
    }
    Assert.assertTrue("expected list to be empty after everything " + "was removed.",
            !list.iterator().hasNext());
}

From source file:org.orekit.frames.FrameTest.java

private Transform randomTransform(Random random) {
    Transform transform = Transform.IDENTITY;
    for (int i = random.nextInt(10); i > 0; --i) {
        if (random.nextBoolean()) {
            Vector3D u = new Vector3D(random.nextDouble() * 1000.0, random.nextDouble() * 1000.0,
                    random.nextDouble() * 1000.0);
            transform = new Transform(transform.getDate(), transform, new Transform(transform.getDate(), u));
        } else {//www .j  a  va2  s. com
            double q0 = random.nextDouble() * 2 - 1;
            double q1 = random.nextDouble() * 2 - 1;
            double q2 = random.nextDouble() * 2 - 1;
            double q3 = random.nextDouble() * 2 - 1;
            double q = FastMath.sqrt(q0 * q0 + q1 * q1 + q2 * q2 + q3 * q3);
            Rotation r = new Rotation(q0 / q, q1 / q, q2 / q, q3 / q, false);
            transform = new Transform(transform.getDate(), transform, new Transform(transform.getDate(), r));
        }
    }
    return transform;
}

From source file:com.ipc.service.SignUpService.java

public StringBuffer makekey() {
    Random rnd = new Random();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < 20; i++) {
        if (rnd.nextBoolean()) {
            buf.append((char) ((int) (rnd.nextInt(26)) + 97));
        } else {/* w  ww  .ja  va  2 s . c  o m*/
            buf.append((rnd.nextInt(10)));
        }
    }
    return buf;
}

From source file:com.ipc.service.SignUpService.java

public String makeNumber(int num) {
    Random rnd = new Random();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < num; i++) {
        if (rnd.nextBoolean()) {
            buf.append((char) ((int) (rnd.nextInt(26)) + 97));
        } else {//from  w  w  w .ja  v  a  2  s  .co m
            buf.append((rnd.nextInt(10)));
        }
    }
    return buf.toString();
}