From 04f7919eb3b0ea3af72cacb0e0c11557a1a15cd8 Mon Sep 17 00:00:00 2001 From: eugenp Date: Mon, 21 Jul 2014 22:08:24 +0300 Subject: [PATCH] java random examples --- .../baeldung/java/CoreJavaRandomUnitTest.java | 56 +++++++++++++++++-- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/CoreJavaRandomUnitTest.java b/core-java/src/test/java/org/baeldung/java/CoreJavaRandomUnitTest.java index 5ba312e08d..17a78651ff 100644 --- a/core-java/src/test/java/org/baeldung/java/CoreJavaRandomUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/CoreJavaRandomUnitTest.java @@ -2,6 +2,7 @@ package org.baeldung.java; import java.util.Random; +import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.math3.random.RandomDataGenerator; import org.junit.Test; @@ -99,17 +100,17 @@ public class CoreJavaRandomUnitTest { } @Test - public void givenUsingPlainJava_whenGeneratingRandomDoubleBounded_thenCorrect() { - final double leftLimit = 1D; - final double rightLimit = 10D; - final double generatedDouble = leftLimit + new Random().nextDouble() * (rightLimit - leftLimit); + public void givenUsingApache_whenGeneratingRandomDoubleUnbounded_thenCorrect() { + final double generatedDouble = new RandomDataGenerator().getRandomGenerator().nextDouble(); System.out.println(generatedDouble); } @Test - public void givenUsingApache_whenGeneratingRandomDoubleUnbounded_thenCorrect() { - final double generatedDouble = new RandomDataGenerator().getRandomGenerator().nextDouble(); + public void givenUsingPlainJava_whenGeneratingRandomDoubleBounded_thenCorrect() { + final double leftLimit = 1D; + final double rightLimit = 10D; + final double generatedDouble = leftLimit + new Random().nextDouble() * (rightLimit - leftLimit); System.out.println(generatedDouble); } @@ -123,4 +124,47 @@ public class CoreJavaRandomUnitTest { System.out.println(generatedDouble); } + // tests - random String + + @Test + public void givenUsingPlainJava_whenGeneratingRandomStringUnbounded_thenCorrect() { + final byte[] array = new byte[7]; // length is bounded by 7 + new Random().nextBytes(array); + final String generatedString = new String(array); + + System.out.println(generatedString); + } + + @Test + public void givenUsingPlainJava_whenGeneratingRandomStringBounded_thenCorrect() { + final int leftLimit = 97; // letter 'a' + final int rightLimit = 122; // letter 'z' + final int targetStringLength = 10; + final StringBuilder buffer = new StringBuilder(targetStringLength); + for (int i = 0; i < targetStringLength; i++) { + final int randomLimitedInt = leftLimit + (int) (new Random().nextFloat() * (rightLimit - leftLimit)); + buffer.append((char) randomLimitedInt); + } + final String generatedString = new String(buffer); + + System.out.println(generatedString); + } + + @Test + public void givenUsingApache_whenGeneratingRandomStringUnbounded_thenCorrect() { + final String generatedString = RandomStringUtils.random(10); + + System.out.println(generatedString); + } + + @Test + public void givenUsingApache_whenGeneratingRandomStringBounded_thenCorrect() { + final int length = 10; + final boolean useLetters = true; + final boolean useNumbers = false; + final String generatedString = RandomStringUtils.random(length, useLetters, useNumbers); + + System.out.println(generatedString); + } + }