Reputation: 3
Here is my problem: I have an array of integers, I would recover 20% of the array elements in a random manner. Is there a function in java to have more than a random value? I tested for only one value:
Random rand=new Random();
int min=0, max=10;
int valeur = rand.nextInt(max - min + 1) + min;
Upvotes: 0
Views: 968
Reputation: 3356
private Integer[] recover(double percentage, Integer[] ints) {
int numberOfElementsToRecover = (int) (ints.length*percentage);
List<Integer> list = Arrays.asList(ints);
Collections.shuffle(list);
return Arrays.copyOfRange(list.toArray(new Integer[]{}), 0, numberOfElementsToRecover);
}
Upvotes: 0
Reputation: 533580
The simplest way to get random but distinct values is to use shuffle.
List<Integer> list = new ArrayList<>();
for(int i = min; i <= max; i++) list.add(i);
Collections.shuffle(list);
You can iterate over list
and get unique numbers in a random order.
You can even using this approach if you want not more than two of something and three of something else by adding the number you wan to the list.
BTW: This approach is O(n) for n numbers whereas generating random numbers and checking if they are there already is O(n^2 * log(n)) which is much slower.
Upvotes: 2
Reputation: 1132
I didnt quite understand the question..
from what i understood you need something like this
List<Integer> listOfRandomNumbers = new ArrayList<Integer>();
//Initialize this List with required numbers
int numberOfRandomNumbersNeeded = listOfRandomNumbers/5;
int min = 0;
int max = listOfRandomNumbers.size() - 1;
Random rand = new Random();
Set<Integer> randomNumbers = new HashSet<Integer>();
while(randomNumbers.size() < numberOfRandomNumbersNeeded) {
int value = rand.nextInt(max - min + 1) + min;
randomNumbers.add(listOfRandomNumbers.get(value));
}
System.out.println(randomNumbers);
Upvotes: 0
Reputation: 159804
This will prevent numbers being used twice and will get 20%:
int[] nums = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Random random = new Random();
int numsFound = 0;
int twentyPercent = (int)(nums.length/5F);
List<Integer> usedIndices = new ArrayList<Integer>();
while (numsFound < twentyPercent) {
int index = random.nextInt(nums.length);
if (!usedIndices.contains(index)) { // not already used?
System.out.println(nums[index]);
usedIndices.add(index);
numsFound++;
}
}
Upvotes: 1
Reputation: 2209
Try This.
import java.util.Random;
public class Dice
{
public static void main(String[] args)
{
// Initialize Variables and Array
Random rand = new Random();
int[] numbers = new int[10];
int min = 0, max = 10;
// Input Random Number Into 10 Elements Of Array
for (int i = 0; i < numbers.length; i++)
numbers[i] = rand.nextInt(max - min + 1) + min;
// Output 10 Array Cells
for (int i = 0; i < numbers.length; i++)
System.out.println(numbers[i]);
}
}
Upvotes: 0
Reputation: 36894
If I understood your question right, you are trying to obtain a "random" array. If so, you can use the following:
int nrOfValues = 20;
int[] values = new int[nrOfValues];
for(int i = 0; i < values.length; i++)
{
values[i] = rand.nextInt(max - min + 1) + min;
}
If you would like to obtain distinct random values, try the following:
ArrayList<Integer> values = new ArrayList<Integer>();
int nrOfValues = 20;
for(int i = 0; i < values.length; i++)
{
int value = rand.nextInt(max - min + 1) + min;
while(values.contains(value))
value = rand.nextInt(max - min + 1) + min;
values.add(value);
}
Of course, if min
and max
are poorly chosen, you might end up with an infinite loop...
Upvotes: 1