I am looking forA random number generator that isBiased towards giving numbers"Furthest away" fromASetof already selected numbers.
For example,ifMy range is[1, -] and I PassinchASetof numbers such as(1, -, +),
Then I would want the generator to"prefer"Producing numbers further away from 1, -, and +.
Therefore, numbers such as -Or -Would is more likely to be drawn than -Or +.
I suspect that Thismay already exist. Does anyone know of such an implementation that I can use forJava?
The
Here is a-a-hand. Basically the idea was we take in some numbers we don't want to generate, then we generate a random number and if that numb Er is in the list of numbers we don ' t want we try again up to a maximum number of retries.
public static void Main (string[] args) {int times = 25; Int[] Listofnumbers = {1, 2, 3}; int max = 5, min = 1; while (times--> 0) {System.out.print (Generatepreferrednumbers (listofnumbers, Max, min) + ""); }}//main method public static Integer generatepreferrednumbers (int[] listofnotpreffered, int max, int min) {Random Rand = new Random (); int randomnum; int retry = 1; Increasing this lessons the likely of We non-preferred numbers to show up hashset<integer> Notprefer = new Hashset<> (); Add all the numbers we don ' t want to generate into a HashSet for easy lookup for (int index = 0; index < listof Notpreffered.length; index++) Notprefer.add (Listofnotpreffered[index]); do {randomnum = Rand.nextint ((max-min) + 1) + min; if (Notprefer.contains (Randomnum)) {retry--; }//wE found a good value, let ' s return it else{retry = 0; }} while (Retry > 0); return randomnum; }
Note:the more times we allow the algorithm to retry the more likely our output would consist of numbers we want. This allows the control how likely you want those non-preferred numbers to show up. This makes sense because if we were to increase the retry to infinite, this would stop only when the number generated is n OT contained in our list non-preferred numbers.
Biased Random number Generator-java