1

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.

2 Answers 2

1

Try this! For 80% for number 1 and 5% for each other up to 5:

Map<Double, Integer> map = new LinkedHashMap<>();
map.put(0.8, 1);
map.put(0.85, 2);
map.put(0.9, 3);
map.put(0.95, 4);
map.put(1, 5);
Random random = new Random();
double result = random.nextDouble();
int value = map.entrySet().stream().filter(entry -> entry.getKey() < result).reduce(0, Math::min);
Sign up to request clarification or add additional context in comments.

Comments

0

edit: solved this as follows:

double percentDupes= 0.2;

//Create an ascending ordered array of size k to be shuffled
int [] tempSorted = new int [filesizes[i]];

for(int k = 0; k < filesizes[i]; k++)
{

    tempSorted[k] = k+1;

}

//Shuffle the ascending array                                                           
shuffleArray(tempSorted);


//Figure out the proportion of the array to replace with duplicate values of elements already in the array 
Double proportion =  tempSorted.length*percentDupes;
int ratio = proportion.intValue();

//Instantiate a new array of size "ratio"
int [] tempShuffled = new int[ratio];

//Fill the new, smaller array with randomly selected elements from the original, sorted array               
for(int b = 0; b< tempShuffled.length; b++)
{

    int randNum = i + (int) (Math.random() * (tempSorted.length - i));  //Select a random element of the array to have as a duplicate               
    tempShuffled[b] = tempSorted[randNum];  //tempSorted was previously shuffled  
}

//Shuffle this sub-array 
shuffleArray(tempShuffled);     

//Loop through (shuffled) original array and, if the values don't match, replace a non-dup with a dup value 

for(int c= 0; c<tempShuffled.length; c++ )
{
    if(tempSorted[c] != tempShuffled[c])
    {

        tempSorted[c] = tempShuffled[c];
    }
}


//Final output to print to file                                             
for(int k = 0; k < tempSorted.length; k++)
{

    System.out.println(tempSorted[k]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.