Source Code : calculate the discrete uniform distribution

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

calculate the discrete uniform distribution

 
import java.util.Random;


/**
 * BeehiveZ is a business process model and instance management system.
 * Copyright (C) 2011  
 * Institute of Information System and Engineering, School of Software, Tsinghua University,
 * Beijing, China
 *
 * Contact: jintao05@gmail.com 
 *
 * This program is a free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation with the version of 2.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


/**
 * @author Tao Jin
 * 
 */

public class Util{

  // calculate the discrete uniform distribution
  // ret[0] stores the number user given
  // ret[1] stores the count of the corresponding number in ret[0] with the
  // same index
  // for some number, the count maybe 0.
  // the parameter validation must be finished in advance.
  public static long[][] getDiscreteUniformDistribution(int min, int max,
      long total) {
    Random rand = new Random(System.currentTimeMillis());
    int span = max - min + 1;
    long avg = total / span;
    long[][] ret = new long[2][span];

    if (avg >= 1) {
      long count = 0;
      for (int i = 0; i < span; i++) {
        ret[0][i] = min + i;
        ret[1][i] = avg;
        count += avg;
      }

      if (count < total) {
        avg = span / (total - count);
        int a = (int) avg;
        int k = 0;
        while (count < total) {
          int i = rand.nextInt(span);
          ret[1][a * k]++;
          k++;
          count++;
        }
      }
    } else {
      for (int i = 0; i < span; i++) {
        ret[0][i] = min + i;
      }
      long count = 0;
      avg = span / total;
      while (count < total) {
        long k = count * avg;
        int kk = (int) k;
        ret[1][kk] = 1;
        count++;
        // int i = rand.nextInt(span);
        // if (ret[1][i] == 0) {
        // ret[1][i] = 1;
        // count++;
        // }
      }
    }
    return ret;
  }


}

   
  

Thank with us