c++ 伪随机数生成

#include <ctime>
#include <cstdlib>
using namespace std;

static unsigned int g_seed = 0;

void seedrand(int seed) {
	g_seed = seed;
}


int rand(int xmin,int xmax) {
	if(xmin>xmax)
		throw "Invalid arguments!";

	if(g_seed==0)
		seedrand((unsigned)time(0));
	g_seed = 214013 * g_seed + 2531011;
	return (g_seed ^ g_seed >> 15)%(xmax-xmin+1)+xmin;
}

posted on 2011-06-26 15:53  大宝pku  阅读(384)  评论(0编辑  收藏  举报

导航