程序1
这个程序就是你要的。
# include "stdio.h"
# include "math.h"
# include "stdlib.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000 /*这个值为N可以定义的最大长度*/
# define N 100 /*产生随机序列的点数,注意不要大于MAX_N*/
/*产生均匀分布的随机变量*/
void randa(float *x,int num)
/*产生瑞利分布的随机变量*/
void randr(float *x,int num)
/*产生标准高斯分布的随机变量*/
void randn(float *x,int num)
/*产生莱斯分布的随机变量*/
void randl(float *x, float a, float b, int num)
void fshow(char *name,float *x,int num)
main()
{
float x[N]
int i
/*
randa(&x,N)
randr(&x,N)
randl(&x,10,10,N)
*/
randn(&x,N)
/*此时x[N]就是所需要的高斯分布的序列*/
/*显示该序列*/
fshow("x",&x,N)
getch()
}
void randa(float *x,int num)
{
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x[i]=rand()
x[i]=x[i]/32768
}
}
void randr(float *x,int num)
{
float x1[MAX_N]
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x1[i]=rand()
x[i]=x1[i]/32768
x[i]=sqrt(-2*log(x[i]))
}
}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N]
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x1[i]=rand()
x2[i]=rand()
x1[i]=x1[i]/32768
x2[i]=x2[i]/32768
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI)
}
}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N]
float temp[MAX_N]
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x1[i]=rand()
x2[i]=rand()
x1[i]=x1[i]/32768
x2[i]=x2[i]/32768
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI)
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI)
x1[i]=temp[i]
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]))
}
}
void fshow(char *name,float *x,int num)
{
int i,sign,L
float temp
printf("\n")
printf(name)
printf(" = ")
L=6
/*按照每行6个数据的格式显示*/
for(i=0i<numi++)
{
temp=i/L
sign=temp
if((i-sign*L)==0) printf("\n")
if(x[i]>0) printf(" %f ",x[i])
else printf("%f ",x[i])
}
}
程序 2
以下程序加入了图形显示的效果,因此更加直观,你可以参考一下。
/* 作者 Leo_nanjing
时间 2008.5.10
功能 生成各种分布的随机变量,并显示
*/
# include "stdio.h"
# include "math.h"
# include "graphics.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000
# define N 1000
void randa(float *x,int num)
void randr(float *x,int num)
void randn(float *x,int num)
void randl(float *x, float a, float b, int num)
void fshow(char *name,float *x,int num)
/*用于图形显示的部分*/
void init_graphic(unsigned color)
void plotxy(float *x, float *y, int num,int mode)
void plot(float *y,int num, int mode)
float max(float *x, int num)
float min(float *x, int num)
/*画出该随机序列的分布函数曲线*/
void plotpdf(float *x,int num,int part,int mode)
main()
{
float x[N]
int i
randn(&x,N)
fshow("x",&x,N)
getch()
/*以下为图形显示部分*/
init_graphic(0)
/*显示随机序列*/
plot(&x,N,1)
getch()
/*显示其分布函数*/
plotpdf(&x,N,20,0)
getch()
}
void randa(float *x,int num)
{
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x[i]=rand()
x[i]=x[i]/32768
}
}
void randr(float *x,int num)
{
float x1[MAX_N]
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x1[i]=rand()
x[i]=x1[i]/32768
x[i]=sqrt(-2*log(x[i]))
}
}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N]
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x1[i]=rand()
x2[i]=rand()
x1[i]=x1[i]/32768
x2[i]=x2[i]/32768
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI)
}
}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N]
float temp[MAX_N]
int i
struct time stime
unsigned seed
gettime(&stime)
seed=stime.ti_hund*stime.ti_min*stime.ti_hour
srand(seed)
for(i=0i<numi++)
{
x1[i]=rand()
x2[i]=rand()
x1[i]=x1[i]/32768
x2[i]=x2[i]/32768
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI)
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI)
x1[i]=temp[i]
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]))
}
}
void fshow(char *name,float *x,int num)
{
int i,sign,L
float temp
printf("\n")
printf(name)
printf(" = ")
L=6
for(i=0i<numi++)
{
temp=i/L
sign=temp
if((i-sign*L)==0) printf("\n")
if(x[i]>0) printf(" %f ",x[i])
else printf("%f ",x[i])
}
}
/*以下为图形显示的函数*/
void init_graphic(unsigned color)
{
int graphicdriver,graphicmode
graphicdriver=DETECT
graphicmode=1
initgraph(&graphicdriver,&graphicmode,"E:\\turboc2\\")
setbkcolor(color)
}
void plotxy(float *x, float*y, int num,int mode)
{
int i
float max_x,max_y,min_x,min_y
float x0,y0,x1,y1
clrscr(0)
cleardevice()
setbkcolor(0)
max_x=max(x,num)
max_y=max(y,num)
min_x=min(x,num)
min_y=min(y,num)
setlinestyle(0,2,1)
line(65,35,65,445)
line(65,445,575,445)
setlinestyle(3,0,1)
line(65,35,575,35)
line(575,35,575,445)
setlinestyle(0,2,1)
if(max_x==min_x)
x0=320
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70
if(max_y==min_y)
y0=240
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40)
if(mode==0) circle(x0,y0,2)
for(i=1i<numi++)
{
if(max_x==min_x)
x1=320
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70
if(max_y==min_y)
y1=240
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40)
if(mode==0) circle(x1,y1,2)
line(x0,y0,x1,y1)
x0=x1y0=y1
}
printf("\n\n")
printf("%f",max_y)
printf("\n\n\n\n\n\n\n\n\n\n")
printf("\n\n\n")
printf("%f",(max_y+min_y)/2)
printf("\n\n\n\n\n\n\n\n\n\n")
printf("\n\n")
printf("%f",min_y)
printf("\n %f",min_x)
printf(" ")
printf("%f",(max_x+min_x)/2)
printf(" ")
printf("%f",max_x)
}
void plot(float*y, int num,int mode)
{
int i
float max_x,max_y,min_x,min_y
float x0,y0,x1,y1
float x[MAX_N]
clrscr(0)
cleardevice()
setbkcolor(0)
for(i=0i<numi++) x[i]=i+1
max_x=max(x,num)
max_y=max(y,num)
min_x=min(x,num)
min_y=min(y,num)
setlinestyle(0,2,1)
line(65,35,65,445)
line(65,445,575,445)
setlinestyle(3,0,1)
line(65,35,575,35)
line(575,35,575,445)
setlinestyle(0,2,1)
if(max_x==min_x)
x0=320
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70
if(max_y==min_y)
y0=240
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40)
if(mode==0) circle(x0,y0,2)
for(i=1i<numi++)
{
if(max_x==min_x)
x1=320
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70
if(max_y==min_y)
y1=240
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40)
if(mode==0) circle(x1,y1,2)
line(x0,y0,x1,y1)
x0=x1y0=y1
}
printf("\n\n")
printf("%f",max_y)
printf("\n\n\n\n\n\n\n\n\n\n")
printf("\n\n\n")
printf("%f",(max_y+min_y)/2)
printf("\n\n\n\n\n\n\n\n\n\n")
printf("\n\n")
printf("%f",min_y)
printf("\n %f",min_x)
printf(" ")
printf("%f",(max_x+min_x)/2)
printf(" ")
printf("%f",max_x)
}
void plotpdf(float *x,int num,int part,int mode)
{
int i,j
float max_x,min_x,round,deltax,up,down,sum
float xl[MAX_N],yl[MAX_N]
sum=0
max_x=max(x,num)
min_x=min(x,num)
round=max_x-min_x
deltax=round/part
xl[0]=min_x
for(i=1i<=parti++)
{
xl[i]=min_x+deltax*i
yl[i-1]=0
up=xl[i]
down=xl[i-1]
for(j=0j<numj++)
{
if((x[j]<up) &&(x[j]>=down)) yl[i-1]=yl[i-1]+1
}
yl[i-1]=yl[i-1]/num/deltax
}
for(i=0i<parti++) sum=sum+yl[i]
plotxy(&xl,&yl,part,mode)
}
float max(float *x, int num)
{
int i
float max
max=x[0]
for(i=1i<numi++)
{
if(x[i]>max) max=x[i]
}
return max
}
float min(float *x, int num)
{
int i
float min
min=x[0]
for(i=1i<numi++)
{
if(x[i]<min) min=x[i]
}
return min
}
US OK-->2010年一级注册结构工程师基础考试大纲?_百度知道 var IKFromImport=false| | G("log_in").src=" http://passport.baidu.com/?login&tpl=ik&u"+escape(location.href)知道 | function ask(fn) (function()Fe.on(window,"resize",A)A()setTimeout(A,100)})()if(!g_f_js) if (typeof(cf)=="function") var IknowPageModify=falsevar UnloadConfirm=}}UnloadConfirm.clear=function()}UnloadConfirm.set()if(!G)return A}}function ctlSubmit(A)}function lockButton(A),3000)}function cReply(A)if(A=="no")}function fixReply(A)function showcpro(A)function ga(B,A)}}elset=h.id}if(t==a||p==a||r==a)window.open(G(a).href,"_blank")}}function ss(A)function cs()var query=(function()D.onsubmit=function()document.forms.ftop.appendChild(E(query.isChanged()))return true}}function A()return B!=document.forms.ftop.word.value}return})()function statFunc(A)function voteAnswer(A)function formSubmit(B,A),B),A||"deal")}var url = escape(location.href)IknowLogParams["page"]="ikqb">>>已解决
2010年一级注册结构工程师基础考试大纲? 悬赏分:0 - 提问时间2010-6-28 16:21谁能提供2010年一级注册结构工程师基础考试大纲啊?我要的是考的内容和分值。 提问者: -
摘要:随机数在实际运用中非常之多,如游戏设计,信号处理,通常我们很容易得到平均分布的随机数。但如何根据平均分布的随机数进而产生其它分布的随机数呢?本文提出了一种基于几何直观面积的方法,以正态分布随机数的产生为例讨论了任意分布的随机数的产生方法。
大家都知道,随机数在各个方面都有很大的作用,在vc的环境下,为我们提供了库函数rand()来产生一个随机的整数。该随机数是平均在0~RAND_MAX之间平均分布的,RAND_MAX是一个常量,在VC6.0环境下是这样定义的:
#define RAND_MAX 0x7fff
它是一个short 型数据的最大值,如果要产生一个浮点型的随机数,可以将rand()/1000.0这样就得到一个0~32.767之间平均分布的随机浮点数。如果要使得范围大一点,那么可以通过产生几个随机数的线性组合来实现任意范围内的平均分布的随机数。例如要产生-1000~1000之间的精度为四位小数的平均分布的随机数可以这样来实现。先产生一个0到10000之间的随机整数。方法如下 :
int a = rand()%10000
然后保留四位小数产生0~1之间的随机小数:
double b = (double)a/10000.0
然后通过线性组合就可以实现任意范围内的随机数的产生,要实现-1000~1000内的平均分布的随机数可以这样做:
double dValue = (rand()%10000)/10000.0*1000-(rand()%10000)/10000.0*1000
则dValue就是所要的值。
到现在为止,你或许以为一切工作都已经完成了,其实不然,仔细一看,你会发现有问题的,上面的式子化简后就变为:
double dValue = (rand()%10000)/10.0-(rand()%10000)/10.0
这样一来,产生的随机数范围是正确的,但是精度不正确了,变成了只有一位正确的小数的随机数了,后面三位的小数都是零,显然不是我们要求的,什么原因呢,又怎么办呢。
先找原因,rand()产生的随机数分辨率为32767,两个就是65534,而经过求余后分辨度还要减小为10000,两个就是20000而要求的分辨率为1000*10000*2=20000000,显然远远不够。下面提供的方法可以实现正确的结果:
double a = (rand()%10000) * (rand()%1000)/10000.0
double b = (rand()%10000) * (rand()%1000)/10000.0
double dValue = a-b
则dValue就是所要求的结果。在下面的函数中可以实现产生一个在一个区间之内的平均分布的随机数,精度是4位小数。
double AverageRandom(double min,double max)
{
int minInteger = (int)(min*10000)
int maxInteger = (int)(max*10000)
int randInteger = rand()*rand()
int diffInteger = maxInteger - minInteger
int resultInteger = randInteger % diffInteger + minInteger
return resultInteger/10000.0
}
但是有一个值得注意的问题,随机数的产生需要有一个随机的种子,因为用计算机产生的随机数是通过递推的方法得来的,必须有一个初始值,也就是通常所说的随机种子,如果不对随机种子进行初始化,那么计算机有一个确省的随机种子,这样每次递推的结果就完全相同了,因此需要在每次程序运行时对随机种子进行初始化,在vc中的方法是调用srand(int)这个函数,其参数就是随机种子,但是如果给一个常量,则得到的随机序列就完全相同了,因此可以使用系统的时间来作为随机种子,因为系统时间可以保证它的随机性。
调用方法是srand(GetTickCount()),但是又不能在每次调用rand()的时候都用srand(GetTickCount())来初始化,因为现在计算机运行时间比较快,当连续调用rand()时,系统的时间还没有更新,所以得到的随机种子在一段时间内是完全相同的,因此一般只在进行一次大批随机数产生之前进行一次随机种子的初始化。下面的代码产生了400个在-1~1之间的平均分布的随机数。
double dValue[400]
srand(GetTickCount())
for(int i= 0i <400i++)
{
double dValue[i] = AverageRandom(-1,1)
}