⑴ 如何在VC++中取得正態分布的隨機數
#include <stdlib.h>
//生成一組-1到1之間的符合標准正態分布的隨機數
//參數:
// double* dRands 用於保存生成的隨機數的數組
// int nCount 數組大小,即隨機數的個數
void Rand(double* dRands, int nCount)
{
srand(GetTickCount());
for(int i = 0; i < nCount; i++)
{
int nRand = rand();//隨機生成0 - 0x7FFF(即0 -- RAND_MAX)的隨機數
double dRand = (double)nRand / RAND_MAX * 2.0 - 1.0;//將隨機數映射到-1 -- 1區間內
dRands[i] = dRand;
}
}
使用例子:
#define RAND_NUM 100 //100個隨機數
//...
double* dRands = new double[RAND_NUM];
Rand(dRands, RAND_NUM);
//...
⑵ 如何產生滿足二元正態分布的隨機數點
試試: random函數。或者:function [data1, data2] = twogaussian(n1,mu1,cov1,n2,mu2,cov2);
%
% [data1, data2] = twogaussian(n1,mu1,sigma1,n2,mu2,sigma2);
%
% Function to simulate data from 2 Gaussian densities in d dimensions
% and to plot the data in the first 2 dimensions
%
% INPUTS:
% n1, n2: two integers, size of data set 1 and 2 respectively
% mu1, mu2: two vectors of dimension 1 x d, means
% for data set 1 and 2
% cov1, cov2: two matrices of dimension d x d, covariance
% matrices for data set 1 and 2 respectively
%
% OUTPUTS:
% data1: n1 x d matrix of data for data set 1
% data2: n2 x d matrix of data for data set 2% check that the dimensionality of the mu's and sigma's are consistent
d1 = length(mu1);
d2 = length(mu2);
if (d1~=d2)
error('means are of different lengths');
end;
d = length(mu1); % d is the dimensionality of the data
[d1 d2] = size(cov1);
if (d1~=d2)
error('cov1 is a non-square covariance matrix');
end;
if (d1~=d)
error('cov1 is of different dimensionality to mu1');
end;
[d1 d2] = size(cov2);
if (d1~=d2)
error('cov2 is a non-square covariance matrix');
end;
if (d1~=d)
error('cov2 is of different dimensionality to mu2');
end;% Call the function mvnrnd.m to generate the two data sets
data1 = mvnrnd(mu1,cov1,n1);
data2 = mvnrnd(mu2,cov2,n2);% Now plot the two data sets as a two-dimensional scatter plot
% if d = 2: plot dimension1 on the xaxis and dimension 2 on the
% yaxis. Plot the points from data1 as green circles 'o', and the
% points from data2 as red crosses 'x'.
if ....
figure % open a figure window
plot(data1(:,1),data1(:,2),'b.');.... % now plot data1
axis([-6 6 -6 6]); % fix the lengths of the axes
hold % hold the figure to overlay a 2nd plot
plot(data2(:,1),data2(:,2),'rx');% now plot data 2
xlabel('Dimension 1');
ylabel('Dimension 2');
title('Simulation of two-class Gaussian data in two dimensions');
endfunction r = mvnrnd(mu,sigma,cases);
%MVNRND Random matrices from the multivariate normal distribution.
% R = MVNRND(MU,SIGMA,CASES) returns a matrix of random numbers
% chosen from the multivariate normal distribution with mean vector,
% MU, and covariance matrix, SIGMA. CASES is the number of rows in R.
%
% SIGMA is a square positive definite matrix with size equal to
% the length of MU% Adapted from Mathworks function[m1 n1] = size(mu);
c = max([m1 n1]);
if m1 .* n1 ~= c
error('Mu must be a vector.');
end[m n] = size(sigma);
if m ~= n
error('Sigma must be square');
endif m ~= c
error('The length of mu must equal the number of rows in sigma.');
end[T p] = chol(sigma);
if p ~= 0
error('Sigma must be a positive definite matrix.');
endif m1 == c
mu = mu';
endmu = mu(ones(cases,1),:);r = randn(cases,c) * T + mu;
⑶ 如何在c語言中生成正態分布的隨機數,要源代碼~謝謝
隨機生成一百個1至100的隨機數。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 100
int main(int argc, char *argv[])
{
int i;
int a[N];
srand(time(NULL));
for(i=0;i<N;i++)
a[i]=rand()%100+1;
printf("生成的隨機數為:\n");
for(i=0;i<N;i++)
{
printf("%5d",a[i]);
if((i+1)%10==0)
printf("\n");
}
system("PAUSE");
return 0;
}
輸出結果如下:
生成的隨機數為:
41 15 82 1 23 51 16 96 92 17
86 71 87 69 74 5 50 18 42 52
46 34 52 18 40 74 79 35 22 36
65 94 80 91 18 72 61 79 4 11
61 30 95 55 11 19 38 87 78 52
95 30 99 53 99 99 10 79 70 33
91 85 10 99 47 58 93 41 19 71
56 60 10 24 73 87 18 38 13 73
57 22 91 4 37 60 67 58 85 48
46 7 57 100 73 96 60 44 24 23
請按任意鍵繼續. . .
⑷ 求大神給出用C語言編程生成正態分布隨機數的程序,要不是標准正態分布的
一般有兩種演算法:
演算法一產生12個(0,1)平均分布的隨機函數,用大數定理可以模擬出正態分布。
演算法二用到了數學中的雅可比變換,直接生成正態分布,但此演算法在計算很大規模的數時
會出現溢出錯誤。
測試程序:
#include <math.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
double _random(void)
{
int a;
double r;
a=rand()%32767;
r=(a+0.00)/32767.00;
return r;
}
double _sta(double mu,double sigma)
{
int i;
double r,sum=0.0;
if(sigma<=0.0) { printf("Sigma<=0.0 in _sta!"); exit(1); }
for(i=1;i<=12;i++)
sum = sum + _random();
r=(sum-6.00)*sigma+mu;
return r;
}
double _sta2(double mu,double sigma)
{
double r1,r2;
r1=_random();
r2=_random();
return sqrt(-2*log(r1))*cos(2*M_PI*r2)*sigma+mu ;
}
int main()
{
int i;
double mu,sigma;
srand( (unsigned)time( NULL ) );
mu=0.0;
sigma=1.0;
printf("Algorithm 1:\n");
for(i=0;i<10;i++)
printf("%lf\t",_sta(mu,sigma));
printf("Algorithm 2:\n");
for(i=0;i<10;i++)
printf("%lf\t",_sta2(mu,sigma));
return 0;
}
//由均勻分布的隨機數得到正態分布的隨機數
#include <math.h>
float gasdev(im)
int *im;
{
static int iset=0;
static float gset;
float fac,r,v1,v2;
float ran1();//產生均勻分布的隨機數,可利用系統函數改寫
if (iset == 0) {
do {
v1=2.0*ran1(im)-1.0;
v2=2.0*ran1(im)-1.0;
r=v1*v1+v2*v2;
} while (r >= 1.0);
fac=sqrt(-2.0*log(r)/r);
gset=v1*fac;
iset=1;
return v2*fac;
} else {
iset=0;
return gset;
}
}
原理可找本數值演算法方面的書看看。
⑸ 標准正態分布函數的c語言代碼 謝啦
double gaussian(double u) //用Box_Muller演算法產生高斯分布的隨機數
{
double r,t,z,x;
double s1,s2;
s1=(1.0+rand())/(RAND_MAX+1.0);
s2=(1.0+rand())/(RAND_MAX+1.0);
r=sqrt(-2*log(s2)/log(e));
t=2*pi*s1;
z=r*cos(t);
x=u+z*N;
return x;
}
以前寫的一個函數,u是均值,N是方差
⑹ c語言正態分布隨機數生成
random.h中的rand_normal函數實現有誤,或者調用有誤。
⑺ 如何生成服從某個二維正態分布的隨機數
是曲線上的點/還是曲線內的點?
曲線上取隨機X,Y為函數值;
⑻ 如何用C語言生成[0.01,2]之間符合正態分布的隨機數。。。注意是正態分布!!答案採用後再追加50分
# include <stdio.h>
# include <math.h>
# include <stdlib.h>
# include <time.h>
# define MAX_N 3000 /*這個值為N可以定義的最大長度*/
# define N 100 /*產生隨機序列的點數,注意不要大於MAX_N*/
# define PI 3.141592653
void randn(double *x,int num)
{
double x1[MAX_N],x2[MAX_N];
int i;
srand((unsigned)time(NULL));
for(i=0;i<N;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/(RAND_MAX+1);
x2[i]=x2[i]/(RAND_MAX+1);
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*2*PI);
}
}
void main()
{
double x[N],x_min,x_max;
int i;
FILE *fp;
if((fp=fopen("test.txt","w+"))==NULL)
{
fprintf(stderr,"Can't open the file ");
exit(1);
}
randn(x,N);
x_min=x[0];
x_max=x[0];
for(i=0;i<N;i++)
{
if(x[i]>x_max)
{
x_max=x[i];
}
if(x[i]<x_min)
{
x_min=x[i];
}
}
for(i=0;i<N;i++)
{
x[i]=(x[i]-x_min)/(x_max-x_min)*(2-0.01)+0.01;
}
for(i=0;i<N;i++)
{
printf("%f ",x[i]);
fprintf(fp,"%lf ",x[i]);
if(i%5==4)
{
printf(" ");
}
}
if(fclose(fp)==EOF)
{
printf("Closing error ");
}
}
把生成的數據放入txt文件中,再導入matlab中,查看是否符合正態分布。
得到H1=0,說明確實是正態分布。。。。
⑼ 高手進,c語言中如何得到服從正態分布的隨機數
最好尋求專門演算法
如果不行的話,也可以用大量隨機數來模擬,譬如生成1000次0或1,然後求其平均數,可以得到很接近正態分布的.
如果有連續的隨機函數,也可以直接求正態分部的積分函數,根據積分函數的反函數來確定位置
⑽ 如何用C語言生成一個正態分布的樣本
調試程序時,隨機數種子可以設常數,例如srand(54321);
用 rand() 產生均勻分布隨機數 x1,x2
利用瑞利分布得正態分布隨機數 y1,y2
再按要求線性縮放一下到[0.01,2] 區間。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
main(){
#define N 100
double rd[N];
double x1,x2,y1,y2;
double pi2=6.28318530728,mx,mi,ave=0;
int i;
//srand(54321);
srand(time(NULL));
for (i=0;i<=N-2;i=i+2){
x1=1.0*rand()/RAND_MAX;
x2=1.0*rand()/RAND_MAX;
y1= sqrt((-2.0*log(x1))) * cos(pi2*x2);
y2= sqrt((-2.0*log(x1))) * sin(pi2*x2);
rd[i]=y1;
rd[i+1]=y2;
}
mx=rd[0];mi=rd[0];
for (i=0;i<N;i++){
if (rd[i]>mx)mx=rd[i];
if (rd[i]<mi)mi=rd[i];
}
//printf("mi=%lf mx=%lf\n",mi,mx);
for (i=0;i<N;i++) rd[i] = (rd[i]-mi)/(mx-mi+0.001) * (2.0-0.01) + 0.01;
for (i=0;i<N-2;i=i+2) printf("%lf %lf\n",rd[i],rd[i+1]);
return 0;
}