mfcheer

c c++ 随机数

版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/41748935

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#include <time.h>//头文件
#include <iostream>

using namespace std;

int main ()
{
	srand(time(NULL));//初始化随机数
	//srand(time(0));//初始化随机数
	//srand(10);//初始化随机数

	for(int i=1;i<=10;i++)
		cout<<rand()%1000<<endl;//生成1000以内的随机数
		//cout<<rand()<<endl;//产生随机数而不需要设定范围的话,只要用rand()就可以了,范围在0至RAND_MAX 间.RAND_MAX定义在stdlib.h, 其值为2147483647.

	srand(time(0));
	int a,b;
	cin>>a>>b;
	int c = double( rand()/RAND_MAX )* (b-a) +a;//生成[a,b]的随机数
	cout<<c<<endl;

	return 0;
}
← 全部文章

留言