生成随机数
来源:互联网 发布:过期域名注册 编辑:程序博客网 时间:2024/05/19 09:04
// 随机数的生成#include <stdio.h>#include <stdlib.h> #include <time.h>#include <iostream>using namespace std;// 随机生成某范围的随机整数,调用前记得srand设置种子int range_random(int start, int end){ return ( start + rand() % (end - start + 1) );}int main(){ float num; int i, length = 10; /* 产生随机数前,必须先利用srand()初始化随机数发生器, * 这样以time函数值(即当前时间,也可以用getpid()),给rand()提供种子, * 如果未设随机数种子,rand()在调用时会自动设随机数种子为1, * 多次运行所得随机数相同(因为rand根据随机数种子生成伪随机序列的) */ srand((unsigned)time(NULL)); // 生成十个(0,length)之间的浮点数 for(i = 0;i < 10;i++) { // rand()会返回一随机数值,范围在0至RAND_MAX 间 // RAND_MAX 在stdlib.h中定义,其值为0x7FFF ,有一注释 /* Maximum value that can be returned by the rand function. */ num = length * ( rand() / (RAND_MAX+1.0) ); cout << num << endl; } cout << endl; // 如果要随机生成某范围的整数 int rd = range_random(3, 9); cout << rd << endl; return 0;}