This example for you to share the Java random number generation code for your reference, the specific contents are as follows
Package com.gonvan.common.utils;
Import java.util.*; /** * Random Number tool * * @author YUERZM * */Public final class Lotteryaliasmethod {/** * random # Generat
or used to sample from the distribution.
* Private Final Random Random;
/** * the alias tables.
* Private Final int[] alias;
/** * the probability tables.
* Private final double[] probability; /** * Constructs a new aliasmethod to sample from a discrete distribution and * hand back outcomes based on the prob
Ability distribution. * <p/> * Given as input a list of probabilities corresponding to outcomes 0, 1, * ..., n-1, this constructor
Creates the probability and alias tables * needed to efficiently sample from this distribution.
* * @param probabilities * The list of probabilities.
* * Public Lotteryaliasmethod (list<double> probabilities) {This (probabilities, new Random ()); }/** * Constructs a new AliasmetHod to sample from a discrete distribution and * hand back outcomes based on the probability distribution. * <p/> * Given as input a list of probabilities corresponding to outcomes 0, 1, * ..., n-1, along with the R
Andom number generator that should to used as * the underlying generator, this constructor creates the probability and
* Alias tables needed to efficiently the sample from this distribution.
* * @param probabilities * The list of probabilities. * @param random * The random number generator/public Lotteryaliasmethod (list<double> probabilities, Random Random) {/* Begin by doing basic structural checks on the inputs. */if (probabilities = null | | Random =
= null) throw new NullPointerException ();
if (probabilities.size () = = 0) throw new illegalargumentexception ("Probability vector must be nonempty."); /* Allocate spaces for the probability and alias tables. * * probability = new DOUBLE[probabilities.size ()];
alias = new Int[probabilities.size ()]; /* Store the underlying generator.
* * This.random = random; /* Compute the average probability and cache it for later use.
* Final Double average = 1.0/probabilities.size ();
* * Make a copy of the probabilities list, since we are making * changes to it.
* * probabilities = new arraylist<double> (probabilities); /* Create two stacks to act as worklists as we populate the tables.
* * deque<integer> small = new arraydeque<integer> ();
deque<integer> large = new arraydeque<integer> (); /* Populate The stacks with the input probabilities. * for (int i = 0; i < probabilities.size (); ++i) {/* If the probability is below the average Proba Bility, then we add * it to the small list;
Otherwise we add it to the large list.
*/if (Probabilities.get (i) >= average) large.add (i); else Small.add (i); } * * As a note:in the mathematical specification of the algorithm, we * 'll always exhaust the small l Ist before the big list.
However, * Due to floating point inaccuracies, this isn't necessarily true. * Consequently, this inner loop (which tries to pair small and large * elements) 'll have to check that both lists a
ren ' t empty. */while (!small.isempty () &&!large.isempty ()) {/* Get the index of the small and the large probabilit ies.
* * int less = Small.removelast ();
int more = Large.removelast (); * * These probabilities have does not yet been scaled the up to be such this * 1/n is given weight 1.0.
We are here instead.
* * Probability[less] = probabilities.get (less) * probabilities.size ();
Alias[less] = more;
* * Decrease the probability of the larger one by the appropriate * amount. * * Probabilities.set (MORE, (Probabilities.get (more) + probabilities.get (less))-average); * * If the new probability is less than the average, add it into the * small list;
Otherwise add it to the large list.
*/if (Probabilities.get (more) >= 1.0/probabilities.size ()) Large.add (more);
else Small.add (more); /* * At this point, everything are in one list, which means and remaining probabilities all Be 1/n. Based in this, set them * appropriately.
Due to numerical issues, we can ' t be sure which * stack would hold the entries, so we empty both.
*/while (!small.isempty ()) probability[small.removelast ()] = 1.0;
while (!large.isempty ()) probability[large.removelast ()] = 1.0;
/** * Samples A value from the underlying distribution.
* * @return A random value sampled from the underlying distribution. */public int Next () {/* Generate a fair die roll to DeterminE which column to inspect.
* * int column = Random.nextint (probability.length); /* Generate a biased coin toss to determine which option to pick.
* * Boolean cointoss = random.nextdouble () < Probability[column]; /* Based on the outcome, return either the column or its alias. * * return Cointoss?
Column:alias[column]; }
}
The above is the entire content of this article, I hope to help you learn.