Biased Random Number Generator - Java

來源:互聯網
上載者:User

標籤:pass   ike   maximum   random   show   sharp   when   cal   ret   

I am looking for a random number generator that is biased towards giving numbers "furthest away" from a set of already selected numbers. 
For example, if my range is [1, 50] and I pass in a set of numbers such as (1, 20, 40),
then I would want the generator to "prefer" producing numbers further away from 1, 20, and 40.
Therefore, numbers such as 50 or 30 would be more likely to be drawn than 20 or 40.
I suspect that this may already exist. Does anyone know of such an implementation that I can use for Java?

 

Here is a way you can do it by hand. Basically the idea is we take in some numbers we don‘t want to generate, then we generate a random number and if that number 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 our 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 < listOfNotPreffered.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 will consist of numbers we want. This allows you to 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 not contained in our list non-preferred numbers.

 

Biased Random Number Generator - Java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.