精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學(xué)習(xí)啦>學(xué)習(xí)英語>專業(yè)英語>計算機英語>

      c中rand的用法

      時間: 長思709 分享

        c中rand的用法你知道嗎?下面小編就跟你們詳細介紹下c中rand的用法,希望對你們有用。

        c中rand的用法如下:

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

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

        srand

        表頭文件

        #include<stdlib.h>

        定義函數(shù)

        int rand(void)

        函數(shù)說明

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

        返回值

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

        范例

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

        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è)置隨機數(shù)種子)

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

        rand

        表頭文件

        #include<stdlib.h>

        定義函數(shù)

        void srand (unsigned int seed);

        函數(shù)說明

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

        返回值

        范例

        /* 產(chǎn)生介于1 到10 間的隨機數(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

      540550