Java Random multSample(Random rng, double[] vals, double normsum)

Here you can find the source of multSample(Random rng, double[] vals, double normsum)

Description

Draw a multinomial sample from (un-normalized) vals

License

Open Source License

Parameter

Parameter Description
rng a parameter
vals a parameter
normsum a parameter

Declaration

public static int multSample(Random rng, double[] vals, double normsum) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**// w w w  . ja  va  2 s  .c  o  m
     * Draw a multinomial sample from (un-normalized) vals
     * 
     * @param rng
     * @param vals
     * @param normsum
     * @return
     */
    public static int multSample(Random rng, double[] vals, double normsum) {
        double rval = rng.nextDouble() * normsum;
        double cumsum = 0;
        int j = 0;
        while (cumsum < rval || j == 0) {
            cumsum += vals[j];
            j++;
        }
        return j - 1;
    }
}

Related

  1. getRandomRequestId(Integer serviceId)
  2. getRandomServices(int maxSize)
  3. getRandomUUID()
  4. isRandomList(List list)
  5. medianHelper(List list, int k, Random r)
  6. permute(Object[] arr, Random random)
  7. rand()
  8. Rand()
  9. randAngle()