真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

c語言隨機分布函數(shù),c語言均勻分布隨機數(shù)

c語言中隨機函數(shù)怎么取1~2x之間的數(shù)

#include stdlib.h

玉泉網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,玉泉網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為玉泉上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的玉泉做網(wǎng)站的公司定做!

這里面的一個方法就是這樣用的。

a= rand()%100 這就表示隨機取到0--100之間的整數(shù)

所以,1-2x

那就

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

就OK了

幫忙用C語言解決下隨機函數(shù)的編程題

完整版純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) //產(chǎn)生隨機數(shù)

main()

{

int a[N],x;

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

srand(time(NULL)); //為隨機數(shù)發(fā)生器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); //產(chǎn)生一個分布在1~100之間的隨機整數(shù)

}

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語言實現(xiàn)瑞利分布,萊斯分布,高斯分布的分布函數(shù)

下面共有兩個程序,程序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 /*產(chǎn)生隨機序列的點數(shù),注意不要大于MAX_N*/

/*產(chǎn)生均勻分布的隨機變量*/

void randa(float *x,int num);

/*產(chǎn)生瑞利分布的隨機變量*/

void randr(float *x,int num);

/*產(chǎn)生標準高斯分布的隨機變量*/

void randn(float *x,int num);

/*產(chǎn)生萊斯分布的隨機變量*/

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個數(shù)據(jù)的格式顯示*/

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);

/*畫出該隨機序列的分布函數(shù)曲線*/

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();

/*顯示其分布函數(shù)*/

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]);

}

}

/*以下為圖形顯示的函數(shù)*/

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;

}

關(guān)于C語言中的隨機函數(shù)rand()的源代碼~~到底怎么求出隨機數(shù)的!!

return一句的功能只是把得數(shù)的絕對值控制在0~32767之間,關(guān)鍵是next = next * 1103515245 + 12345一句。next變量是靜態(tài)的,變化以后的值是不消失的,下一次還可用,所以每執(zhí)行一次next = next * 1103515245 + 12345就獲得一個新值,這個新值被return取整除、取余除后控制在0~32767之間就形成了0~32767的隨機數(shù)。比如第一次,next=1*1103515245 + 12345=1103527590,經(jīng)return中的對65536取整、對32768取余后就是16838;若再來一次,next已經(jīng)是1103527590了,那么next = next * 1103515245 + 12345就為next = 1103527590 * 1103515245 + 12345=1217759518843121895;但這個數(shù)已經(jīng)溢出了,實際上表示成了-1770082073,經(jīng)return中取整取余后返回的就是-27009;可以算出來,再下一次是10113……不過,你這個函數(shù)并不能產(chǎn)生真正意義上的“隨機”數(shù),因為作為基數(shù)的1103515245在每次開始時是不變的,所以只能得出同樣的序列,即每次執(zhí)行都產(chǎn)生16838、-27009、10113……這樣一組無限多的數(shù)。所以實際的C隨機函數(shù)rand()的形參并不是void而是一個長整型變量,通常來調(diào)用實時時間函數(shù)獲取實時時間值來得到,因為時間是每時每刻都在變化的,所以充當"1103515245”角色的數(shù)就每調(diào)用一次的值都不同。這樣每次執(zhí)行就都能得到不重復(fù)的序列。至于12345,我想隨便取個數(shù)都行!僅供參考……

C語言:通過隨機函數(shù)產(chǎn)生100個( 0--100 )之間的整數(shù),統(tǒng)計其中各個數(shù)據(jù)段的數(shù)據(jù)數(shù)量,已經(jīng)給出框架?

#include stdio.h

#include time.h

#include stdlib.h

//產(chǎn)生n個隨機數(shù),依次放在x指向的內(nèi)存空間

void funRand(int *x, int n) {

srand((unsigned) time(NULL));

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

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

}

}

//統(tǒng)計x指向的內(nèi)存空間中數(shù)據(jù)的分段數(shù)量,保存在以y指向的內(nèi)存空間

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執(zhí)行的內(nèi)存空間中的n個數(shù)據(jù)

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); //產(chǎn)生隨機數(shù)

funCount(a, 100, num); //統(tǒng)計

printf("產(chǎn)生的隨機數(shù)為:\n");

arrayOut(a, 100); //輸出數(shù)據(jù)

printf("各隨機數(shù)區(qū)間分布:\n");

arrayOut(num, 11); //輸出統(tǒng)計數(shù)據(jù)

return 0;

}

求大神給出用C語言編程生成正態(tài)分布隨機數(shù)的程序,要不是標準正態(tài)分布的

一般有兩種算法:

算法一產(chǎn)生12個(0,1)平均分布的隨機函數(shù),用大數(shù)定理可以模擬出正態(tài)分布。

算法二用到了數(shù)學(xué)中的雅可比變換,直接生成正態(tài)分布,但此算法在計算很大規(guī)模的數(shù)時

會出現(xiàn)溢出錯誤。

測試程序:

#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;

}

//由均勻分布的隨機數(shù)得到正態(tài)分布的隨機數(shù)

#include math.h

float gasdev(idum)

int *idum;

{

static int iset=0;

static float gset;

float fac,r,v1,v2;

float ran1();//產(chǎn)生均勻分布的隨機數(shù),可利用系統(tǒng)函數(shù)改寫

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;

}

}

原理可找本數(shù)值算法方面的書看看。


網(wǎng)頁名稱:c語言隨機分布函數(shù),c語言均勻分布隨機數(shù)
路徑分享:http://weahome.cn/article/hdcjoc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部