[C++]C++随机函数[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 bgu 的 blog

c++随机函数(vc program):

#include <stdio.h>

#include <iostream>

#include <time.h>

using namespace std;

#define max 100

int main(int argc, char* argv[])

{

       srand( (unsigned)time( null ) );         //srand()函数产生一个以当前时间开始的随机种子

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

              cout<<rand()%max<<endl;         //max为最大值,其随机域为0~max-1

       return 0;

}

 


rand()通常的使用方法是这样的:

rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数。

这样,如果你要产生0~1010个整数,可以表达为:

int n = rand() % 11;

本文关键:[C++]C++随机函数
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top