I have the following code generating 5-digit random numbers and adding them to an ArrayList. These numbers however must be unique ids.
for(int i = 0; i < myArr.length; i++) {
int id = (int) (Math.round(Math.random() * 89999) + 10000);
idArr.add(id);
}
I'm trying to work out how I could check to see if the number was already in the array before adding it but I can't get my head around the best way to do this.
Don't use an (Array)List, use a Set:
Set<Integer> set = ...;
while (set.size() < myArr.length) {
set.add(yourRandomNumber);
}
Use an ArrayList instead of an array. That way you would just need to use ArrayList#contains(obj) method to test whether the id is already in the ArrayList or not.
Or, you can just work with a HashSet, which will work faster with its HashSet#contains() method.
You can create a Set of the numbers. E.g.:
Set<Integer> intSet = new HashSet<Integer>();
while(intSet.size() < myArr.length) {
intSet.add(getNextRandomInt());
}
Then yo can do anything with that Set.
So, if you need an array, just call:
Integer[] intArray = intSet.toArray(new Integer[myArr.length]);
or, if you need an ArrayList or int[] array:
// ArrayList:
List<Integer> ints = new ArrayList<Integer>();
ints.addAll(intSet);
// int[] array:
int[] intArray = new int[myArr.length];
for( int i = 0; i<intArray.length; ++i) {
intArray[i] = int.get(i);
}
Anything which involves looping until you find enough unique random numbers is never guaranteed to complete. It's possible, if extremely unlikely, that the random number generator will never output enough unique numbers in a reasonable amount of time.
(I know this will never happen in practice, but it's a theoretical possibility.)
A safe alternative is to choose one random number, and then increment it in a loop until you have enough numbers.
int n = new Random().nextInt(89999 - myArr.length) + 10000;
for (int i = 0; i < myArr.length; i++) {
idArr.add(n++);
}
Related
This question already has answers here:
Creating random numbers with no duplicates
(20 answers)
Closed 1 year ago.
This is probably already asked, but it is a little difficult for me to understand. I created a for loop to add random integers into my array, but when the integer is already in the array, restart the loop. But I keep on getting the same integers into the array + when there already is a duplicate, the array size increases. Does anyone know what I'm doing wrong?
Random r = new Random();
int[] tes = new int[5];
for (int i = 0; i < tes.length; i++) {
int q = r.nextInt(10);
for (int j = 0; j < i; j++) {
if (q == tes[j]){
i=i-1;
}
}
tes[i] = q;
System.out.println(tes[i]);
}
and the output:
If you want a collection without duplicates you should use a Set:
Random r = new Random();
int desirableSize = 5;
Set<Integer> uniques = new HashSet<>(desirableSize);
while(uniques.size() < desirableSize){
uniques.add(r.nextInt(10));
}
System.out.println(uniques);
The add method already ensures that a value is not added if it already exist on the set.
boolean add(E e)
Adds the specified element to this set if it is not already present (optional operation).
I have used HashSet, however if the insertion order is important for you, use instead LinkedHashSet:
As pjs have pointed out the aforementioned approach is good when:
desirableSize is much less than the pool size, but if the
desirableSize is a substantial proportion of pool size you're better
off shuffling the pool and selecting the first desirableSize elements.
Something as follows:
int start = 0;
int end = 10;
int size = 5;
List<Integer> collect = IntStream.rangeClosed(start, end)
.boxed()
.limit(size)
.collect(Collectors.toList());
Collections.shuffle(collect);
System.out.println(collect);
The rational is the following (quoting pjs):
With rejection-based schemes such as repeated attempts to add to a
set, the expected number of iterations is O(poolsize *
log(desirableSize)) with O(desirableSize) storage for the set.
Shuffling is O(poolsize) but requires O(poolsize) storage for the
shuffle. As desirableSize -> poolsize, shuffling wins on expected
iterations and becomes competitive on storage. With partial shuffling
implementations, the number of iterations for shuffling is
O(desirableSize) although the storage remains the same.
Or more informally, the higher it is the unique finite set of numbers that will be pickup from a finite set of possible numbers, the more desirable it is to use the second approach.
For instance, if one generates numbers from 0 to 1000 and is only interested in 5 numbers, since the probability of picking randomly the same numbers is lower, one is better off with the first approach. However, if you would be (instead) interested in 800 numbers, then one would be better off generating and shuffling the 1000 numbers, and extracting the 800 unique values from them.
Memory-wise the first approach is better then second approach, however performance-wise it depends in the context as we have already described.
i dont see a problem.
Your System.out.println(tes[i]); is in loop
your array has only following ints: 5,9,2,7,1
make println in own loop
for (int i = 0; i < tes.length; i++) {
System.out.println(tes[i]);
}
because you make i=i-1; one value is printed many times
I managed to solve it in a different way:
List<Integer> tes = new ArrayList<>(5);
Random r = new Random();
for (int i = 0; i < 5; i++) {
int testNummer = r.nextInt(10);
if(!tes.contains(testNummer)) {
tes.add(testNummer);
System.out.println(testNummer);
}else{
i=i-1;
}
}
this way is more efficient, I have noticed.
Some logic problem
Increment i variable when you store value in array and don't decrement i variable just break inner loop when found duplicate.
when duplicate found then restart outer loop. use if else condition for that
try below code and for simple understanding i have changed outer loop in while
int i = 0;
while(i<5)
{
int q = r.nextInt(10);
System.out.println("random value generated"+ q );
int j=0;
for (;j < i; j++)
{
if (q == tes[j])
{
System.out.println("duplicate value found"+ q );
break;
}
}
if(j!=i)
{
continue;
}
else
{
if(j==i)
{
tes[i] = q;
i=i+1;
}
}
System.out.println("value at "+ tes[i]);
}
If you want an easy way to generate unique values you can do it with a stream.
Random r = new Random();
int minVal = 1;
int upperBound = 20;
int count = 10;
As long as count is less than upperBound - minVal it will finish without duplicates. For very large counts with the appropriate range, it may take some time.
int[] unique = r.ints(minVal, upperBound).distinct().limit(count).toArray();
System.out.println(Arrays.toString(unique));
Prints something like this.
[14, 1, 7, 13, 5, 16, 2, 8, 12, 4]
An easy way to generate random numbers of a fixed range is to simply shuffle the array.
Integer[] vals = new Integer[20];
for (int i = 0; i < vals.length; i++) {
vals[i] = i+1;
}
// Object array will be shuffle since it backs up the list.
Collections.shuffle(Arrays.asList(vals));
System.out.println(Arrays.toString(vals));
Prints something like
[7, 20, 5, 10, 17, 18, 3, 13, 11, 1, 2, 8, 4, 9, 19, 12, 15, 16, 6, 14]
This question already has answers here:
Java Array of unique randomly generated integers
(10 answers)
Closed 5 years ago.
i want to create an array of 10000 unique random elements. Till now i only figure out how to create random integers and fill an array and finding the doubles and deleted them. But this decrease the size of the array which i dont want it.
So the question is how i can fill an array with unique integers as elements without decreasing the size of the array.
You could use this code. Usage of Set will eliminate duplicates and you are fetching random numbers until you get 10000 different random integers.
Set<Integer> numbers = new HashSet<>();
Random r = new Random();
while (numbers.size() < 10000) {
numbers.add(r.nextInt(100000));
}
Integer[] a = new Integer[numbers.size()];
a = numbers.toArray(a);
I found this great solution:
This solution doesn't need any Collection class.
public static int[] createRandomNumbers(int howMany) {
int n = howMany + 1;
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i;
}
int [] result = new int[n];
int x = n;
SecureRandom rd = new SecureRandom();
for (int i = 0; i < n; i++) {
int k = rd.nextInt(x);
result[i] = a[k];
a[k] = a[x-1];
x--;
}
return result;
}
System.out.println(Arrays.toString(createRandomNumbers(10000)));
Reference: Best way to create a list of unique random numbers in Java
Hope it helps
Try this logic:
USE AN ARRAYLIST ENTIRELY, THEN CONVERT TO AN ARRAY AT THE END OF THE ENTIRE OPERATION.
Declare an arraylist
For every random number generated, check if the number already exists in the arraylist (using the .contains() method). If it does, repeat the process, else, move to the next number.
Code example:
Arraylist<Integer> arr = new Arraylist<>();
arr.add(generate()); //I included this line so that the arraylist won't be empty
//Note that the method *generate()* generates a new random number
for(int i = 0; i < 9999; i++){
int next = generate(); //the method that generates your number
if(arr.contains(next)){
i--; //The entire operation will be repeated for this index.
}
else{
arr.add(next); //Add the number to the arraylist
}
}
int[] finalArray = arr.toArray(); //Your final resultant array!
I hope this helps.. Merry coding!
You can use Set. This Collection that contains no duplicate elements.
Documentation https://docs.oracle.com/javase/7/docs/api/java/util/Set.html
Set<Integer> numbers = new HashSet();
do {
numbers.add(ThreadLocalRandom.current().nextInt());
} while(numbers.size() < 10000);
Is there a way where you can use Math.random to prints the element in a given array?
int[] list = new int[] {1,2,3};
So the output will be like
2,1,3
or
3,1,2
or
2,3,1
Perhaps you can approach it by shuffling your array then print it. If the original should not be modified, you can make a copy and then shuffle the copy.
There are well-known algorithms for shuffling array (or a deck of cards). One can be found here. An implementation in java looks like this:
static void shuffleArray(int []array) {
int length = array.length;
for (int i = length -1; i > 0; i--) {
// generate a random 0 <= j < i
int j = (int)(Math.random() * i);
// swap elements at i and j
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
The approach proposed in most answers is extremely inefficient, as it works in O(N2) time. Think about it: at first you'll generate unused indexes with one attempt, but closer to the end, when almost all array is processed, it will require nearly N steps to generate next unused index.
The optimal O(N) approach is to create shuffled array of indexes (0..N) where each index appears only once and then process your original array in the order of shuffled indexes. Each step requires O(N) time, so the whole algorithm is O(N).
int[] input = new int[]{5, 4, 3, 6, 2, 1};
int []indices = new int[input.length];
//Fisher-Yates shuffle
Random rnd = new Random();
for (int i = 0; i < indices.length; i++) {
int j = rnd.nextInt(i + 1);
indices[i] = indices[j];
indices[j] = i;
}
for (int i : indices) {
System.out.println(input[i]);
}
I didn't use Collections.shuffle, as it would require usage of Collection and thus wrapped Integer elements, which is very inefficient comparing to the plain int array.
Also, if you are ok with modifying your original array, you can just shuffle it in place (using the same Fisher-Yates shuffle) and then consume it while traversing.
UPD: Replaced shuffling array of indices with shuffled initialization.
Since you have java 8, you can take advantage of the beautiful Stream API.
In short, you can do:
new Random().ints(1, 500).limit(500).forEach(p -> System.out.println(list[p]));
Where 1 is the lowest int generated (inclusive) and 500 is the highest (exclusive). limit means that your stream will have a length of 500, maybe in that argument you want to put list.length.
For your case:
int[] list = new int[] {1,2,3,4,5,6};
new Random().ints(0, list.length).limit(10).forEach(p -> System.out.println(list[p]));
Prints: 5 2 5 4 6 3 3 5 6 4 (Obviously will not print the same numbers for you)
Create a random integer that may be as high as the length of the array - 1. If the random integer is equal to a previous used random integer -- known by storing used integers in an array -- create a new random integer. Otherwise, print the string correlated with that index specified by the random integer. If the length of the array storing the used random integers is equal to the length of the array of strings, stop the process.
This should print all your strings only once each and randomly.
Here is the solution
public static void main(String[] args) {
int[] list = new int[] { 1, 2, 3 };
int[] aux = new int[list.length];
int countTimes = 0;
while (countTimes < list.length) {
int position = new Random().nextInt(list.length);
if (aux[position] != list[position]) {
System.out.println(list[position]);
aux[position] = list[position];
countTimes++;
}
}
}
As I said in the comments. This answer will work.. All you need to do is track the indices that it accessed so you don't repeat them or remove that element from the array.
void printRandom(int[] array) {
if (array.length == 0)
return;
Random rand = new Random();
int rnd = rand.nextInt(array.length);
int element = array[rnd];
array = ArrayUtils.removeElement(array, element);
System.out.print(element);
printRandom(array);
}
Just repeat this process until all elements are removed. Obviously add checks to prevent errors and keep in mind I haven't used JAVA in a long time so post back if you have issues!
Lastly keep in mind this deletes the array so you may want to wrap this code in a function and then copy the array as a local variable so you can reuse the original as needed
In this case we can print random value from array using like below :
Steps:
Create list object of Integer to hold printed indices
Get random number and check whether this index is already printed or not
if not printed then add it in list and print value from array using this index
if list size and array length is equal then terminate the loop
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RandomIndices {
public static void main(String[] args) {
int[] list = new int[]{1, 2, 3};
Random random = new Random();
List<Integer> randomIndices = new ArrayList<>(); //to hold indices which are already printed
boolean isRemain = true;
while (isRemain) {
int randomIndex = random.nextInt(list.length);
if (!randomIndices.contains(randomIndex)) { //check random index value of array is printed or not
randomIndices.add(randomIndex);
System.out.println(list[randomIndex]);
}
if (randomIndices.size() == list.length) {
isRemain = false;
}
}}
}
Implement a simple "do while" statement to prevent duplicate numbers from showing up out of your array (I used a StringArray - but an IntegerArray would work the same way - as a side note, I can place the complete code up here but didn't want to do so if it didn't apply. I use a drop-down to select how many random words to generate - then display that set of true RANDOM words (non-repeated):
final Random rand1 = new Random();
final Random rand2 = new Random();
final int rndInt1 = rand1.nextInt(getResources().getStringArray(R.array.words).length);
int rndInt2 = rand2.nextInt(getResources().getStringArray(R.array.words).length);
if (rndInt1 == rndInt2){
do {
rndInt2 = rand2.nextInt(getResources().getStringArray(R.array.words).length);
}while (rndInt1 == rndInt2);//if indexes are equal - re-run the array search
}
outString = getResources().getStringArray(R.array.words)[rndInt1];
outString += ", " + getResources().getStringArray(R.array.words)[rndInt2];//concatenate the list
textWord = (TextView) findViewById(R.id.textWords);//An empty text field in my layout
textWord.setText(outString);//Set that empty text field to this string of random array elements
I've created an ArrayList for integers which I would like to fill with 200 numbers. Each number can be within a range between 0 and 1023.
Therefore I've written this code:
Random rand = new Random();
ArrayList<Integer> values = new ArrayList<Integer>();
int START_AMOUNT = 200;
for(int i = 0; i < START_AMOUNT;
values.add(rand.nextInt(1024));
}
As You might see, the for-loop will add 200 random numbers to the "values" ArrayList, from 0 to 1023. Now my problem is that I want the Array to have only unique numbers. How can I tell the Random class not to generate any numbers that already are existent in the ArrayList?
What I'd do is creating an array of 1023 int composed by 1,2,3,...,1023. Then you shuffle it, and you take only the 200 first terms :
List<Integer> ints = new ArrayList<Integer>();
for(int i = 1; i <= 1023; i++)
{
ints.add(i);
}
Collections.shuffle(ints);
EDIT as suggested by #Bohemian♦
List<Integer> result = ints.subList(0,200);
A Set is a Collection that cannot contain duplicate elements.
It models the mathematical set abstraction.
The Set interface contains only methods inherited from Collection
and adds the restriction that duplicate elements are prohibited.
And therefore,
public boolean add(E e)
Adds the specified element to this set if it is not already present.
[...]
If this set already contains the element,
the call leaves the set unchanged and returns false.
As such, what I'd do is use a Set, then add those to the list:
List<Integer> values = new ArrayList<Integer>();
Set<Integer> set = new HashSet<Integer>();
while(set.size() < 200)
{
set.add(rand.nextInt(1024));
}
values.addAll(set);
Use a Set:
Random rand = new Random();
Set<Integer> values = new HashSet<Integer>();
final int START_AMOUNT = 200;
while(values.size() < START_AMOUNT) {
values.add(rand.nextInt(1024));
}
List<Integer> uniqueList = new ArrayList<Integer>(values);
System.out.println(uniqueList);
You could also check if the ArrayList contains the given random number everytime you want to add one.
Random rand = new Random();
Integer r;
ArrayList<Integer> values = new ArrayList<Integer>();
int START_AMOUNT = 200;
for(int i = 0; i < START_AMOUNT; i++) {
r = rand.nextInt(1024);
If !values.contains(r) {
values.add(r);
} else {
i--;
}
}
Although i think Kabulan0lak's answer would be more performant if that is important.
The task is described here Creating combination of numbers or here Permutations of integer array algorithm.
I decided to solve this problem in this way:
From first number to the last check the possibility of permutation:
Create arraLists of the first permutation and of the difference
del1 and rem0
Bool is an object with two parametrs: st - start and end
It`s new permutation
I create new Bool, that at the start have the same start and end, like it is one number. So, we cant use this number anymore before starting new permutation with another start number. And we remove it from the list of numbers rem . And start create permutation with checking.
for (int index = 0; index < arr.length; index++) {
List<Integer> del1 = new ArrayList<Integer>();
List<Integer> rem0 = new ArrayList<Integer>();
for (int i = 0; i < del.length; i++) {
del1.add(del[i]);
}
for (int i = 0; i < arr.length; i++) {
rem0.add(arr[i]);
}
Bool start = new Bool(arr[index], arr[index]);
rem0.remove(index);
check(start, del1, rem0);
}
Checking:
ucet - number of "good" permutations, if the difference of new permutation is the same, as the first, it is "good".
So, I for the ending number I add difference number and if it = number in the list of possible numbers - I add this number to the end of permutation, remove it from the list of possible and remove difference prom the list of differences(by creating new lists).
And then continue creating.
private static void check(Bool start, List<Integer> del2, List<Integer> rem) {
if (del2.isEmpty()) {
ucet++;
}
for (int index = 0; index<rem.size(); index++) {
for (int i = 0; i<del2.size(); i++) {
if(start.end+del2.get(i)==rem.get(index)){
List<Integer> del3 = new ArrayList<Integer>();
List<Integer> rem2 = new ArrayList<Integer>();
del3=del2;rem2=rem;
Bool con=new Bool(start.st,rem.get(index));
rem2.remove(index);
del3.remove(i);
check(con,del3,rem2);
}
}
}
}
But I have one bug, that I cant understand. Its IndexOutOfBoundsException. In the string if(start.end+del2.get(i)==rem.get(index)){ .
And it causes del2.get(i).
But the problem is, that i<del2.size().
Can you help me to deal with it? Thanks
You are removing entities from the list as you are looping through it, not good. You could try using while loop in conjunction with iterable.hasNext() or instead of rem0.remove(index) do rem0.set(index, null).