random.cpp

来自「Scalable k-means software and test datas」· C++ 代码 · 共 110 行

CPP
110
字号
/* Scalable K-means clustering softwareCopyright (C) 2000  Fredrik Farnstrom and James LewisThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.See the file README.TXT for more information.*//* random.cpp */#include <math.h>#include "random.h"#define RNDMASK 0x7fff#define M1 259200#define IA1 7141#define IC1 54773#define RM1 (1.0/M1)#define M2 134456#define IA2 8121#define IC2 28411#define RM2 (1.0/M2)#define M3 243000#define IA3 4561#define IC3 51349static unsigned long next_rand = 1;extern "C" int time(int);// Generate a pseudo random number in the range from 0.0 to 1.0.REAL random(void){  next_rand = next_rand * 1812433253L + 12345L;  return ((int)(next_rand >> 16) & RNDMASK) * 2.0 / 65535;}// Set seed value for the pseudo random number generator.void seed(int s){  next_rand = (unsigned long int)s;}RandomGenerator::RandomGenerator(void){	seed(time(0));}RandomGenerator::RandomGenerator(int initseed){	seed(initseed);}void RandomGenerator::seed(int s){	nextRand = s;//	srand48(s);}REAL RandomGenerator::random(void){  nextRand = nextRand*1812433253L + 12345L;  return ((int)(nextRand >> 16) & RNDMASK) * 2.0/65535;}unsigned long RandomGenerator::randomInt(void){	nextRand = nextRand*1812433253L + 12345L;	return ((int)(nextRand >> 16) & RNDMASK);//	return lrand48();}unsigned long RandomGenerator::randomInt(unsigned long range){	nextRand = nextRand * 1812433253L + 12345L;	return (unsigned long)((range*((int)(nextRand >> 16) & RNDMASK))/			(RNDMASK+1.0));//	return lrand48();}unsigned long RandomGenerator::maxRandomInt(void){	return RNDMASK;//	return 0x80000000;}RandomGenerator RandomGenerator::defaultRandomGenerator;/* End of file random.cpp */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?