BAEL-5678 - Creating Random Numbers With No Duplicates in Java (#12698)

* BAEL-5678 - Creating Random Numbers With No Duplicates in Java

Ready for review.

* BAEL-5678 - review

* using `isEmpty()` for `hasNext()`;
* using `assertEquals` instead of `assertTrue`;
This commit is contained in:
Ulisses Lima 2022-10-26 03:44:35 -03:00 committed by GitHub
parent 14f14f5c00
commit 8ad8a38682
3 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.baeldung.uniquerng;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
public class BigUniqueRng implements Iterator<Integer> {
private Random random = new Random();
private Set<Integer> generated = new LinkedHashSet<>();
public BigUniqueRng(int size, int max) {
while (generated.size() < size) {
Integer next = random.nextInt(max);
generated.add(next);
}
}
@Override
public Integer next() {
if (!hasNext())
throw new NoSuchElementException();
Iterator<Integer> iterator = generated.iterator();
Integer next = iterator.next();
iterator.remove();
return next;
}
@Override
public boolean hasNext() {
return !generated.isEmpty();
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.uniquerng;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class UniqueRng implements Iterator<Integer> {
private int size;
private List<Integer> numbers = new ArrayList<>();
public UniqueRng(int size, boolean zeroBased) {
this.size = size;
int start = (zeroBased ? 0 : 1);
int limit = (zeroBased ? size - 1 : size);
for (int i = start; i <= limit; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
}
@Override
public Integer next() {
if (!hasNext())
throw new NoSuchElementException();
return numbers.remove(0);
}
@Override
public boolean hasNext() {
return !numbers.isEmpty();
}
public int getSize() {
return size;
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.uniquerng;
import static org.junit.Assert.assertEquals;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.junit.Test;
public class RngUtilsUnitTest {
@Test
public void whenNUniqueNumRequested_thenNUniqueNumConsumed() {
TreeSet<Integer> set = new TreeSet<>();
int n = 5;
UniqueRng rng = new UniqueRng(n, true);
while (rng.hasNext()) {
set.add(rng.next());
}
assertEquals(n, set.size());
}
@Test
public void givenYRange_whenNUniqueNumRequested_thenNUniqueNumConsumed() {
TreeSet<Integer> set = new TreeSet<>();
int n = 5;
int y = n * 10;
BigUniqueRng rng = new BigUniqueRng(n, y);
while (rng.hasNext()) {
set.add(rng.next());
}
assertEquals(n, set.size());
}
@Test
public void givenIntStreamSizeN_whenCollected_thenSetSizeN() {
int n = 5;
int from = -5;
int to = n * 2;
Random r = new Random();
Set<Integer> set = r.ints(from, to)
.distinct()
.limit(n)
.boxed()
.collect(Collectors.toSet());
assertEquals(n, set.size());
}
}