C语言中随机函数发生器rand()用法举例

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#if 1
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    int nRandNumber[10];
    for (int idx = 0; idx < 10; ++idx)
    {
        nRandNumber[idx] = rand();//取随机数范围为integer 0 ~ RAND_MAX(32767)
        cout << nRandNumber[idx] << endl;
    }
    for (int idx = 0; idx < 10; ++idx)
    {
        nRandNumber[idx] = (double)rand() / (RAND_MAX + 1) * (125 - 35) + 35;//取数35到125之间
        cout << nRandNumber[idx] << endl;
    }
    cout << RAND_MAX << endl;//最大值是32767
 
     
    //srand是一个随机数种子产生函数,当参数为1时,所得到的随机数序列一样
    //output : nRanNum1 : 41 nRanNum2 : 18467
    //         nRanNum3 : 41 nRanNum4 : 18467
    ofstream outFile("d:\out.txt");
 
    srand(1);
    int nRanNum1 = rand();
    int nRanNum2 = rand();
 
    cout << "nRanNum1 : " << nRanNum1 << " nRanNum2 : " << nRanNum2 << endl;
     
    outFile << "nRanNum1 : " << nRanNum1 << " nRanNum2 : " << nRanNum2 << endl;
    srand(1);
    int nRanNum3 = rand();
    int nRanNum4 = rand();
    cout << "nRanNum3 : " << nRanNum3 << " nRanNum4 : " << nRanNum4 << endl;
    outFile << "nRanNum3 : " << nRanNum3 << " nRanNum4 : " << nRanNum4 << endl;
     
    system("pause");
    return 1;
     
}
#endif