java2组随机数的共通数_java随机数产生-指数分布 正态分布 等
1 指數分布
指數分布的概率密度函數:y=lamda*exp(-lamda*x)
x>=0
由此可以計算概率分布函數:y=1-exp(-lamda*x)
x>=0
y是 ?X
首先,把y當作是在(0,1)區間的均勻分布的隨機變量。
然后,求y=1-exp(-lamda*x)的逆函數,x=-(1/lamda)*ln(1-y)
令z=1-y,顯然z也是(0,1)區間的均勻分布的隨機變量,于是就有x=-(1/lamda)*ln(z)。
z可以通過(double)
rand() ?/ ?RAND_MAX計算。原因是rand() ?是隨機分布函數。
最終滿足指數分布的變量x,就可以通過x=-(1/lamda)*ln(z)計算。
import
java.util.*;
public class zhishu
{
public
static void main(String[] args) {
double x,
z;
double
lamda;
System.out.println("請輸入lamda的值:");
Scanner
scanner = new Scanner(System.in);
lamda =
scanner.nextDouble();
for(int i=0;
i<10; i++) {
z =
Math.random();
x = -(1 /
lamda) * Math.log(z);
System.out.println(x);
}
}
}
----------------------------------------------------------------------
2
正態分布
java.util.Random里的nextGaussian(),生成的數值符合均值為0方差為1的高斯/正態分布,即符合標準正態分布。
產生數字的范圍:任何數都有可能,不過在0左右的數字較多。
產生N(a,b)的數:Math.sqrt(b)*random.nextGaussian()+a; 即均值為a,方差為b的隨機數
--------------------------------------------------------------------------------------
3 擴展:使用[0,1]之間的均勻分布生成任意一個分布函數的隨機數序列
任務:給定一個分布的密度函數f(x),要生成滿足這一分布的一組隨機數。
輸入:一組[0,1]之間的滿足均勻分布的隨機數U
輸出:一組滿足f(x)的隨機數V
方法:1)求f(x)的分布函數F(x)
2)求F(x)的反函數F'(x)
3)對于U中的每一個元素u,將F'(u)加入序列V中。
4 其他:colt庫中有隨機數發生器
cern.jet.random.Distributions
Method Summary
static?double
k,
double?p)?Returns
the probability distribution function of the discrete geometric
distribution.
static?double
r,
int?nr,?RandomEngine?randomGenerator)?Returns
a random number from the Burr II, VII, VIII, X Distributions.
static?double
r,
double?k,
int?nr,?RandomEngine?randomGenerator)?Returns
a random number from the Burr III, IV, V, VI, IX, XII
distributions.
static?double
Returns
a cauchy distributed random number from the standard Cauchy
distribution C(0,1).
static?double
variance,
double?mean,?RandomEngine?randomGenerator)?Returns
an erlang distributed random number with the given variance and
mean.
static?int
p,?RandomEngine?randomGenerator)?Returns
a discrete geometric distributed random
number;
static?double
l3,
double?l4,?RandomEngine?randomGenerator)?Returns
a lambda distributed random number with parameters l3 and l4.
static?double
Returns
a Laplace (Double Exponential) distributed random number from the
standard Laplace distribution L(0,1).
static?double
Returns
a random number from the standard Logistic distribution
Log(0,1).
static?double
alpha,
double?cut,?RandomEngine?randomGenerator)?Returns
a power-law distributed random number with the given exponent and
lower cutoff.
static?double
Returns
a random number from the standard Triangular distribution in
(-1,1).
static?double
alpha,
double?beta,?RandomEngine?randomGenerator)?Returns
a weibull distributed random number.
static?int
z,?RandomEngine?randomGenerator)?Returns
a zipfian distributed random number with the given skew.
總結
以上是生活随笔為你收集整理的java2组随机数的共通数_java随机数产生-指数分布 正态分布 等的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计量经济学计算机输出结果,计量经济学作业
- 下一篇: linux部署tomcat项目404_T