⑴ 如何在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;
}