/************************************************************************
 * AUTHOR: NiuJiuRu
 * FILENAME: swrand.c
 * DESCRIPTION: 随机数产生函数
 * NOTE:
 * HISTORY:
 * 1, [2010-08-23] created by NiuJiuRu
 ************************************************************************/
#include "swapi.h"
#include "swrand.h"

/* 产生一个特定范围内的随机数, 并返回结果 */
unsigned int xrand32(unsigned int range)
{
	struct timespec ts = {0};
	unsigned int randNum = 0;

	if((int)range < 0) return -1;

	clock_gettime(CLOCK_REALTIME, &ts);
	srand((unsigned int)(ts.tv_sec + ts.tv_nsec + rand()));

	if(range == 0) randNum = (unsigned int)((rand() << 16) | rand());
	else randNum = ((unsigned int)((rand() << 16) | rand())) % range;
	
	return randNum;
}
