Biased Random number Generator-java

Source: Internet
Author: User
Tags generator rand

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.