Generating random integers within range with a probability distribution

前端 未结 2 669
旧巷少年郎
旧巷少年郎 2020-12-31 22:48

I have a problem where I want to generate a set of random integer values between 1 and 5 inclusive using a probability distribution.

Poisson and Inverse Gamma are t

2条回答
  •  礼貌的吻别
    2020-12-31 23:34

    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 data=new ArrayList();
    
                for(int i=0;i<1000;i++){
                    data.add(useFunction(rnd.nextDouble()));
                }
    
            }
    
            public double useFunction(double in){
                return in*in;
            }
        }
    

提交回复
热议问题