在java中实现确定性分布
我已经为二项分布编写了以下代码,有人能告诉我如何对确定性分布(该分布必须始终生成相同的数字)执行相同的操作吗?它应该是最简单的分布,但我在库中找不到“确定性分布” 谢谢你的帮助在java中实现确定性分布,java,Java,我已经为二项分布编写了以下代码,有人能告诉我如何对确定性分布(该分布必须始终生成相同的数字)执行相同的操作吗?它应该是最简单的分布,但我在库中找不到“确定性分布” 谢谢你的帮助 import java.util.HashMap; import org.apache.commons.math3.distribution.AbstractIntegerDistribution; import org.apache.commons.math3.distribution.BinomialDistribu
import java.util.HashMap;
import org.apache.commons.math3.distribution.AbstractIntegerDistribution;
import org.apache.commons.math3.distribution.BinomialDistribution;
public class Binomial extends Distribution
{
AbstractIntegerDistribution distribution;
@Override
public void setParameters(HashMap<String,?> hm)
{
try
{
int n = 0;
double p =0.0;
if (hm.containsKey("n"))
n = Integer.parseInt((String) hm.get("n"));
else
throw new Exception("Exception: No n-value found");
if(hm.containsKey("p"))
p = Double.parseDouble((String) hm.get("p"));
else
throw new Exception("Exception: No p-value found");
distribution = new BinomialDistribution(n,p);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
@Override
public int getSample()
{
int a = distribution.sample();
return a;
}
public AbstractIntegerDistribution getDistribution()
{
return distribution;
}
}
import java.util.HashMap;
导入org.apache.commons.math3.distribution.AbstractIntegerDistribution;
导入org.apache.commons.math3.distribution.BinomialDistribution;
公共类二项分布
{
抽象整数分布分布;
@凌驾
公共void setParameters(HashMap hm)
{
尝试
{
int n=0;
双p=0.0;
如果(hm.CONTANSKEY(“n”))
n=Integer.parseInt((字符串)hm.get(“n”));
其他的
抛出新异常(“异常:未找到n值”);
如果(hm.CONTANSKEY(“p”))
p=Double.parseDouble((字符串)hm.get(“p”));
其他的
抛出新异常(“异常:未找到p值”);
分布=新的二元分布(n,p);
}
捕获(例外e)
{
System.out.println(e.getMessage());
}
}
@凌驾
公共int getSample()
{
int a=分布.sample();
返回a;
}
public AbstractIntegerDistribution getDistribution()
{
收益分配;
}
}
我不知道是否有用于此的现有库,但一种方法是使用构造函数
public Random(long seed)
因为这将返回由种子单独确定的Random
实例
public static int binomialObservation(long seed, int n, double p) {
if (n < 0)
throw new IllegalArgumentException();
if (p <= 0 || n == 0)
return 0;
if (p >= 1)
return n;
Random random = new Random(seed);
int count = 0;
for (int i = 0; i < n; i++)
if (random.nextDouble() <= p)
count++;
return count;
}
公共静态int-binomialObservation(长种子,int-n,双p){
if(n<0)
抛出新的IllegalArgumentException();
如果(p=1)
返回n;
随机随机=新随机(种子);
整数计数=0;
对于(int i=0;i