I am trying to create (in Java) an integer array containing a specified number of integers, k, (for example, 50, 500, 1000, etc.) that does not use library functions or Collections, but contains a random assortment of numbers in the range (i.e. from 1 to k), with a specified percentage of duplicates.
I have figured out how to implement the shuffle, but I am not sure how to best implement the dups threshold. What I have so far:
static void shuffle(int[] array) {
int n = array.length;
for (int i = 0; i < array.length; i++) {
// Get a random index of the array past i.
int random = i + (int) (Math.random() * (n - i));
// Swap the random element with the present element.
int randomElement = array[random];
array[random] = array[i];
array[i] = randomElement;
}
}
Then, using this method:
//Create an ascending ordered array of size k to be shuffled
int [] tempInit = new int [filesizes[i]];
for(int k = 0; k < filesizes[i]; k++)
{
tempInit[k] = k+1;
}
//Shuffle the ascending array
shuffle(tempInit);
for(int k = 0; k < tempInit.length; k++)
{
System.out.println(tempInit[k]);
}
One way I imagine it could work would be to use the percentage of duplicates required (let's say it's 20%) and then randomly select a fixed number of integers, and loop through the array and replace every element (if not equal to one of the fixed numbers) with one of these fixed numbers. I'm not sure what sort of logic would make sense to select these fixed numbers, or the amount of fixed numbers to use for replacement purposes.