六月丁香五月婷婷,丁香五月婷婷网,欧美激情网站,日本护士xxxx,禁止18岁天天操夜夜操,18岁禁止1000免费,国产福利无码一区色费

學(xué)習(xí)啦 > 學(xué)習(xí)英語 > 專業(yè)英語 > 計(jì)算機(jī)英語 > c語言中rand的用法

c語言中rand的用法

時(shí)間: 長思709 分享

c語言中rand的用法

  c語言中rand的用法的用法你知道嗎?下面小編就跟你們?cè)敿?xì)介紹下c語言中rand的用法的用法,希望對(duì)你們有用。

  c語言中rand的用法的用法如下:

  rand(產(chǎn)生隨機(jī)數(shù))

  相關(guān)函數(shù)

  srand

  表頭文件

  #include<stdlib.h>

  定義函數(shù)

  int rand(void)

  函數(shù)說明

  rand()會(huì)返回一隨機(jī)數(shù)值,范圍在0至RAND_MAX 間。在調(diào)用此函數(shù)產(chǎn)生隨機(jī)數(shù)前,必須先利用srand()設(shè)好隨機(jī)數(shù)種子,如果未設(shè)隨機(jī)數(shù)種子,rand()在調(diào)用時(shí)會(huì)自動(dòng)設(shè)隨機(jī)數(shù)種子為1。關(guān)于隨機(jī)數(shù)種子請(qǐng)參考srand()。

  返回值

  返回0至RAND_MAX之間的隨機(jī)數(shù)值,RAND_MAX定義在stdlib.h,其值為2147483647。

  范例

  /* 產(chǎn)生介于1 到10 間的隨機(jī)數(shù)值,此范例未設(shè)隨機(jī)數(shù)種子,完整的隨機(jī)數(shù)產(chǎn)生請(qǐng)參考

  srand()*/

  #include<stdlib.h>

  main()

  {

  int i,j;

  for(i=0;i<10;i++)

  {

  j=1+(int)(10.0*rand()/(RAND_MAX+1.0));

  printf("%d ",j);

  }

  }

  執(zhí)行

  9 4 8 8 10 2 4 8 3 6

  9 4 8 8 10 2 4 8 3 6

  srand(設(shè)置隨機(jī)數(shù)種子)

  相關(guān)函數(shù)

  rand

  表頭文件

  #include<stdlib.h>

  定義函數(shù)

  void srand (unsigned int seed);

  函數(shù)說明

  srand()用來設(shè)置rand()產(chǎn)生隨機(jī)數(shù)時(shí)的隨機(jī)數(shù)種子。參數(shù)seed必須是個(gè)整數(shù),通??梢岳胓eypid()或time(0)的返回值來當(dāng)做seed。如果每次seed都設(shè)相同值,rand()所產(chǎn)生的隨機(jī)數(shù)值每次就會(huì)一樣。

  返回值

  范例

  /* 產(chǎn)生介于1 到10 間的隨機(jī)數(shù)值,此范例與執(zhí)行結(jié)果可與rand()參照*/

  #include<time.h>

  #include<stdlib.h>

  main()

  {

  int i,j;

  srand((int)time(0));

  for(i=0;i<10;i++)

  {

  j=1+(int)(10.0*rand()/(RAND_MAX+1.0));

  printf(" %d ",j);

  }

  }

  執(zhí)行

  5 8 8 8 10 2 10 8 9 9

  2 9 7 4 10 3 2 10 8 7

533329