[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
This commit is contained in:
Harsha Veeravalli 2019-08-29 06:46:47 +02:00 committed by maibin
parent 8d67d8496a
commit bfd84477ee
4 changed files with 136 additions and 0 deletions

View File

@ -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<Integer> getNumbersInRange(int start, int end) {
List<Integer> result = new ArrayList<>();
for (int i = start; i < end; i++) {
result.add(i);
}
return result;
}
public List<Integer> getNumbersUsingIntStreamRange(int start, int end) {
return IntStream.range(start, end)
.boxed()
.collect(Collectors.toList());
}
public List<Integer> getNumbersUsingIntStreamRangeClosed(int start, int end) {
return IntStream.rangeClosed(start, end)
.boxed()
.collect(Collectors.toList());
}
public List<Integer> getNumbersUsingIntStreamIterate(int start, int limit) {
return IntStream.iterate(start, i -> i + 1)
.limit(limit)
.boxed()
.collect(Collectors.toList());
}
}

View File

@ -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();
}
}

View File

@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> numbers = numbersInARange.getNumbersUsingIntStreamIterate(1, 10);
assertEquals(Arrays.asList(1,2,3,4,5,6,7,8,9,10), numbers);
}
}

View File

@ -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);
}
}