您的位置:首页 > 编程语言 > C语言/C++

C++产生m到n之间的随机数,产生0到100之间的随机数,以系统时间作为随机种子

2011-10-28 20:52 501 查看
// 随机数产生.cpp : Defines the entry point for the console application.

//
#include "stdafx.h"

#include<iostream>

#include<ctime>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{

int i =0 ;

cout<<"产生10个0到100之间的随机数:"<<endl;

srand((unsigned)time(NULL)); //以系统时间作为随机种子

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

{

cout<<rand()%100<< " ";

}

cout<<endl;

cout<<"请输入需要产生m到n之间的随机数的m与n的值:"<<endl;

int m,n;

cin>>m>>n;

cout<<"产生10个 "<<m<<" 到 "<<n<<" 之间的随机数:"<<endl;

srand((unsigned)time(NULL)); //以系统时间作为随机种子

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

{

cout<<m + rand()%(n-m+1)<< " ";

}

cout<<endl;

system("pause");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐