From bfd84477ee18459cc62eaaf8ad230ad5d0b9addb Mon Sep 17 00:00:00 2001 From: Harsha Veeravalli Date: Thu, 29 Aug 2019 06:46:47 +0200 Subject: [PATCH] [BAEL-3211] Generate Integers within a range in Java (#7668) * [BAEL-3211] Generate Integers within a range in Java * [BAEL-3211] Generate Integers within a range in Java * Moved files from java-numbers to java-numbers-2 --- .../numbersinrange/NumbersInARange.java | 36 ++++++++++++++++ .../numbersinrange/RandomNumbersInARange.java | 22 ++++++++++ .../NumbersInARangeUnitTest.java | 43 +++++++++++++++++++ .../RandomNumbersInARangeUnitTest.java | 35 +++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 java-numbers-2/src/main/java/com/baeldung/numbersinrange/NumbersInARange.java create mode 100644 java-numbers-2/src/main/java/com/baeldung/numbersinrange/RandomNumbersInARange.java create mode 100644 java-numbers-2/src/test/java/com/baeldung/numbersinrange/NumbersInARangeUnitTest.java create mode 100644 java-numbers-2/src/test/java/com/baeldung/numbersinrange/RandomNumbersInARangeUnitTest.java diff --git a/java-numbers-2/src/main/java/com/baeldung/numbersinrange/NumbersInARange.java b/java-numbers-2/src/main/java/com/baeldung/numbersinrange/NumbersInARange.java new file mode 100644 index 0000000000..937583cdb5 --- /dev/null +++ b/java-numbers-2/src/main/java/com/baeldung/numbersinrange/NumbersInARange.java @@ -0,0 +1,36 @@ +package com.baeldung.numbersinrange; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class NumbersInARange { + + public List getNumbersInRange(int start, int end) { + List result = new ArrayList<>(); + for (int i = start; i < end; i++) { + result.add(i); + } + return result; + } + + public List getNumbersUsingIntStreamRange(int start, int end) { + return IntStream.range(start, end) + .boxed() + .collect(Collectors.toList()); + } + + public List getNumbersUsingIntStreamRangeClosed(int start, int end) { + return IntStream.rangeClosed(start, end) + .boxed() + .collect(Collectors.toList()); + } + + public List getNumbersUsingIntStreamIterate(int start, int limit) { + return IntStream.iterate(start, i -> i + 1) + .limit(limit) + .boxed() + .collect(Collectors.toList()); + } +} diff --git a/java-numbers-2/src/main/java/com/baeldung/numbersinrange/RandomNumbersInARange.java b/java-numbers-2/src/main/java/com/baeldung/numbersinrange/RandomNumbersInARange.java new file mode 100644 index 0000000000..dc6ddfe781 --- /dev/null +++ b/java-numbers-2/src/main/java/com/baeldung/numbersinrange/RandomNumbersInARange.java @@ -0,0 +1,22 @@ +package com.baeldung.numbersinrange; + +import java.util.Random; + +public class RandomNumbersInARange { + + public int getRandomNumber(int min, int max) { + return (int) ((Math.random() * (max - min)) + min); + } + + public int getRandomNumberUsingNextInt(int min, int max) { + Random random = new Random(); + return random.nextInt(max - min) + min; + } + + public int getRandomNumberUsingInts(int min, int max) { + Random random = new Random(); + return random.ints(min, max) + .findFirst() + .getAsInt(); + } +} diff --git a/java-numbers-2/src/test/java/com/baeldung/numbersinrange/NumbersInARangeUnitTest.java b/java-numbers-2/src/test/java/com/baeldung/numbersinrange/NumbersInARangeUnitTest.java new file mode 100644 index 0000000000..3225257166 --- /dev/null +++ b/java-numbers-2/src/test/java/com/baeldung/numbersinrange/NumbersInARangeUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.numbersinrange; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +public class NumbersInARangeUnitTest { + + @Test + public void givenTheRange1To10_andUsingForLoop_thenExpectCorrectResult() { + NumbersInARange numbersInARange = new NumbersInARange(); + List numbers = numbersInARange.getNumbersInRange(1, 10); + + assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9), numbers); + } + + @Test + public void givenTheRange1To10_andUsingIntStreamRange_thenExpectCorrectResult() { + NumbersInARange numbersInARange = new NumbersInARange(); + List numbers = numbersInARange.getNumbersUsingIntStreamRange(1, 10); + + assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9), numbers); + } + + @Test + public void givenTheRange1To10_andUsingIntStreamRangeClosed_thenExpectCorrectResult() { + NumbersInARange numbersInARange = new NumbersInARange(); + List numbers = numbersInARange.getNumbersUsingIntStreamRangeClosed(1, 10); + + assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9,10), numbers); + } + + @Test + public void givenTheRange1To10_andUsingIntStreamIterate_thenExpectCorrectResult() { + NumbersInARange numbersInARange = new NumbersInARange(); + List numbers = numbersInARange.getNumbersUsingIntStreamIterate(1, 10); + + assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9,10), numbers); + } +} diff --git a/java-numbers-2/src/test/java/com/baeldung/numbersinrange/RandomNumbersInARangeUnitTest.java b/java-numbers-2/src/test/java/com/baeldung/numbersinrange/RandomNumbersInARangeUnitTest.java new file mode 100644 index 0000000000..77b2cbbfef --- /dev/null +++ b/java-numbers-2/src/test/java/com/baeldung/numbersinrange/RandomNumbersInARangeUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.numbersinrange; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class RandomNumbersInARangeUnitTest { + + @Test + public void givenTheRange1To10_andUsingMathRandom_thenExpectCorrectResult() { + RandomNumbersInARange randomNumbersInARange = new RandomNumbersInARange(); + int number = randomNumbersInARange.getRandomNumber(1, 10); + + assertTrue(number >= 1); + assertTrue(number < 10); + } + + @Test + public void givenTheRange1To10_andUsingRandomInts_thenExpectCorrectResult() { + RandomNumbersInARange randomNumbersInARange = new RandomNumbersInARange(); + int number = randomNumbersInARange.getRandomNumberUsingInts(1, 10); + + assertTrue(number >= 1); + assertTrue(number < 10); + } + + @Test + public void givenTheRange1To10_andUsingRandomNextInt_thenExpectCorrectResult() { + RandomNumbersInARange randomNumbersInARange = new RandomNumbersInARange(); + int number = randomNumbersInARange.getRandomNumberUsingNextInt(1, 10); + + assertTrue(number >= 1); + assertTrue(number < 10); + } +}