Generating random integers within range with a probability distribution

て烟熏妆下的殇ゞ 提交于 2019-11-30 09:44:19

From your problem description, it sounds like you actually want a sample generated from a discrete probability distribution, and you can use EnumeratedIntegerDistribution for this purpose. Choose appropriate probabilities for each of your integers, maybe something like the following would meet your needs:

int[] numsToGenerate           = new int[]    { 1,   2,    3,   4,    5   };
double[] discreteProbabilities = new double[] { 0.1, 0.25, 0.3, 0.25, 0.1 };

EnumeratedIntegerDistribution distribution = 
    new EnumeratedIntegerDistribution(numsToGenerate, discreteProbabilities);

int numSamples = 100;
int[] samples = distribution.sample(numSamples);

Just tweak the discreteProbabilities values to whatever you require.

I would just produce uniformly distributed random numbers then pass them into the distribution function you want, so if the distribution function was x^2

import java.util.ArrayList;

import java.util.Random;



    public class Test{

        public static void main(String args[]){
            Test t=new Test();
        }

        public Test(){

            Random rnd=new Random();

            ArrayList<Double> data=new ArrayList<Double>();

            for(int i=0;i<1000;i++){
                data.add(useFunction(rnd.nextDouble()));
            }

        }

        public double useFunction(double in){
            return in*in;
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!