C语言中随机函数发生器rand()用法举例
2018-07-20 来源:open-open
#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
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:C语言经典算法 - 字符串核对
下一篇:iOS开发-AsynSocket
最新资讯
热门推荐