Mega Code Archive

 
Categories / Java / Development Class
 

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;   } }