I have two arrays of equal length, one with a set of integers, one with a set of doubles. What I'm trying to write is a program getRandom() which takes in the two arrays as input and returns every number of the first array with the probability as specified in the second array at the corresponding index. This if I have the following array, A = { 2, 6, 19, 5}
and the probabilities as B = { 0.2, 0.3, 0.25, 0.25 }
Then I want the function getRandom to return 2
at a probability of 0.2
and so on.
This is the logic that I have worked out so far,
public int getRandom(int[] arr, int[] freq) {
//Instantiate a random number generator
Random rand = new Random();
//generate the index between 0 and 3
int index = rand.nextInt(3); //This needs to be modified to generate the indexed at a particular frequency.
//return the number at the index
return arr[index];
}
Can someone help me in getting the bias into the function?
Aucun commentaire:
Enregistrer un commentaire