c语言随机分布函数,c语言均匀分布随机数

c语言中随机函数怎么取1~2x之间的数

#include stdlib.h

玉泉网站建设公司创新互联公司,玉泉网站设计制作,有大型网站制作公司丰富经验。已为玉泉上1000家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的玉泉做网站的公司定做!

这里面的一个方法就是这样用的。

a= rand()%100 这就表示随机取到0--100之间的整数

所以,1-2x

那就

a= 1 + rand()%(2*x);

就OK了

帮忙用C语言解决下随机函数的编程题

完整版纯C程序:

#includestdio.h

#includestdlib.h

#includetime.h

#define N 20

void sel_sort(int [],int); //选择排序

int search(int [],int,int); //查找

void insert(int [],int [],int,int); //插入

int num_by_mechine(void) //产生随机数

main()

{

int a[N],x;

int i,index,b[N+1];

srand(time(NULL)); //为随机数发生器rand()播种

for(i=0;iN;i++)

a[i]=num_by_mechine();

printf("before sel_sort...\n");

for(i=0;iN;i++)

printf("%4d",a[i]);

printf("\n");

sel_sort(a,N);

printf("after sel_sort...\n");

for(i=0;iN;i++)

printf("%4d",a[i]);

printf("\n");

printf("please input x:");

scanf("%d",x);

index=search(a,x,N);

if(index!=-1)

{

printf("find x,x=a[%d]!\n",index);

printf("delete a[%d]...\n",index);

for(i=index+1;iN;i++)

a[i-1]=a[i];

for(i=0;iN-1;i++)

printf("%4d",a[i]);

}

else

{

printf("no find!\n");

printf("after insert...\n");

insert(a,b,x,N);

for(i=0;iN+1;i++)

printf("%4d",b[i]);

}

}

int num_by_mechine(void)

{

return(rand()%100+1); //产生一个分布在1~100之间的随机整数

}

void sel_sort(int a[],int n)

{

int cur,i,t;

for(cur=0;curn-1;cur++)

for(i=cur;in;i++)

if(a[i]a[cur])

t=a[cur],a[cur]=a[i],a[i]=t;

}

int search(int a[],int x,int n)

{

int front=0,back=n-1,middle;

while(front=back)

{

middle=(front+back)/2;

if(xa[middle])

back=middle-1;

else if(xa[middle])

front=middle+1;

else

return(middle);

}

return -1;

}

void insert(int a[],int b[],int x,int n)

{

int i,j;

for(i=0;in;i++)

b[i]=a[i];

for(i=0;in;i++)

if(a[i]x)

{ for(j=n;ji;j--)

b[j]=b[j-1];

b[i]=x;

return;

}

b[n]=x;

}

用C语言实现瑞利分布,莱斯分布,高斯分布的分布函数

下面共有两个程序,程序2 加入了图形显示

程序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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=0;inum;i++)

{

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=1;inum;i++)

{

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=x1;y0=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=0;inum;i++) 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=1;inum;i++)

{

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=x1;y0=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=1;i=part;i++)

{

xl[i]=min_x+deltax*i;

yl[i-1]=0;

up=xl[i];

down=xl[i-1];

for(j=0;jnum;j++)

{

if((x[j]up) (x[j]=down)) yl[i-1]=yl[i-1]+1;

}

yl[i-1]=yl[i-1]/num/deltax;

}

for(i=0;ipart;i++) sum=sum+yl[i];

plotxy(xl,yl,part,mode);

}

float max(float *x, int num)

{

int i;

float max;

max=x[0];

for(i=1;inum;i++)

{

if(x[i]max) max=x[i];

}

return max;

}

float min(float *x, int num)

{

int i;

float min;

min=x[0];

for(i=1;inum;i++)

{

if(x[i]min) min=x[i];

}

return min;

}

关于C语言中的随机函数rand()的源代码~~到底怎么求出随机数的!!

return一句的功能只是把得数的绝对值控制在0~32767之间,关键是next = next * 1103515245 + 12345一句。next变量是静态的,变化以后的值是不消失的,下一次还可用,所以每执行一次next = next * 1103515245 + 12345就获得一个新值,这个新值被return取整除、取余除后控制在0~32767之间就形成了0~32767的随机数。比如第一次,next=1*1103515245 + 12345=1103527590,经return中的对65536取整、对32768取余后就是16838;若再来一次,next已经是1103527590了,那么next = next * 1103515245 + 12345就为next = 1103527590 * 1103515245 + 12345=1217759518843121895;但这个数已经溢出了,实际上表示成了-1770082073,经return中取整取余后返回的就是-27009;可以算出来,再下一次是10113……不过,你这个函数并不能产生真正意义上的“随机”数,因为作为基数的1103515245在每次开始时是不变的,所以只能得出同样的序列,即每次执行都产生16838、-27009、10113……这样一组无限多的数。所以实际的C随机函数rand()的形参并不是void而是一个长整型变量,通常来调用实时时间函数获取实时时间值来得到,因为时间是每时每刻都在变化的,所以充当"1103515245”角色的数就每调用一次的值都不同。这样每次执行就都能得到不重复的序列。至于12345,我想随便取个数都行!仅供参考……

C语言:通过随机函数产生100个( 0--100 )之间的整数,统计其中各个数据段的数据数量,已经给出框架?

#include stdio.h

#include time.h

#include stdlib.h

//产生n个随机数,依次放在x指向的内存空间

void funRand(int *x, int n) {

srand((unsigned) time(NULL));

for (int i = 0; i n; i++) {

  *(x + i) = rand() % (n + 1);

}

}

//统计x指向的内存空间中数据的分段数量,保存在以y指向的内存空间

void funCount(int *x, int n, int *y) {

int count_09 = 0;

int count_1019 = 0;

int count_2029 = 0;

int count_3039 = 0;

int count_4049 = 0;

int count_5059 = 0;

int count_6069 = 0;

int count_7079 = 0;

int count_8089 = 0;

int count_9099 = 0;

int count_100 = 0;

for (int i = 0; i n; i++) {

  if (*(x + i) = 0 *(x + i) 10)

      *y = ++count_09;

  if (*(x + i) = 10 *(x + i) 20)

      *(y + 1) = ++count_1019;

  if (*(x + i) = 20 *(x + i) 30)

      *(y + 2) = ++count_2029;

  if (*(x + i) = 30 *(x + i) 40)

      *(y + 3) = ++count_3039;

  if (*(x + i) = 40 *(x + i) 50)

      *(y + 4) = ++count_4049;

  if (*(x + i) = 50 *(x + i) 60)

      *(y + 5) = ++count_5059;

  if (*(x + i) = 60 *(x + i) 70)

      *(y + 6) = ++count_6069;

  if (*(x + i) = 70 *(x + i) 80)

      *(y + 7) = ++count_7079;

  if (*(x + i) = 80 *(x + i) 90)

      *(y + 8) = ++count_8089;

  if (*(x + i) = 90 *(x + i) 100)

      *(y + 9) = ++count_9099;

  if (*(x + i) == 100)

      *(y + 10) = ++count_100;

}

}

//输出x执行的内存空间中的n个数据

void arrayOut(int *x, int n) {

int m = 0;

if (n == 11) {

  printf("0-9\t10-19\t20-29\t30-39\t40-49\t50-59\t60-69\t70-79\t80-89\t90-99\t100\n");

  for (int i = 0; i n; i++) {

      printf("%d\t", *(x + i));

  }

} else {

  for (int i = 0; i n; i++) {

      m = m + 1;

      printf("%d\t", *(x + i));

      if (m % 10 == 0)

          printf("\n");

  }

}

printf("\n");

}

int main() {

int i, a[100], num[11] = {0};

funRand(a, 100); //产生随机数

funCount(a, 100, num); //统计

printf("产生的随机数为:\n");

arrayOut(a, 100); //输出数据

printf("各随机数区间分布:\n");

arrayOut(num, 11); //输出统计数据

return 0;

}

求大神给出用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;i10;i++)

printf("%lf\t",_sta(mu,sigma));

printf("Algorithm 2:\n");

for(i=0;i10;i++)

printf("%lf\t",_sta2(mu,sigma));

return 0;

}

//由均匀分布的随机数得到正态分布的随机数

#include math.h

float gasdev(idum)

int *idum;

{

static int iset=0;

static float gset;

float fac,r,v1,v2;

float ran1();//产生均匀分布的随机数,可利用系统函数改写

if (iset == 0) {

do {

v1=2.0*ran1(idum)-1.0;

v2=2.0*ran1(idum)-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语言随机分布函数,c语言均匀分布随机数
分享路径:http://azwzsj.com/article/hdcjoc.html