⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 random.cpp

📁 rand.rar 随机数生成C++代码
💻 CPP
字号:
/**************************************************************************** File Name  : random.c Purpose    : random number generator  Release    : Version 1.0 Date	    : Aug 31,1995GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang TechnologicalUniversity (NTU), Singapore. These software programs are available to the user without any license or royalty fees. Permission is hereby granted to use, copy, modify, and distribute this software and  its documentation for any purpose. ITI and NTU gives no warranty, express, implied,  or statuary for the software and/or documentation provided, including, without  limitation, waranty of merchantibility and warranty of fitness for a particular  purpose. The software provided hereunder is on an "as is"  basis, and ITI and NTU  has no obligation to provide maintenance, support, updates, enhancements, or  modifications.GSNAKE API is available for any UNIX compatible system. User feedback, bugs, or software and manual suggestions should be sent via electronic mail to one of the following, who may or may not act on them as he/she desires :		asschan@ntu.ac.sg		kflai@iti.gov.sg***************************************************************************/#include "stdafx.h"#include "gsn.h"#include "time.h"#include <stdio.h>#include <stdlib.h>/* use system clock to initialize random number generator */void rand_init(){	struct tm tp ;	//struct timezone tzp ;	_getsystime(&tp) ;		srand(tp.tm_sec) ;}/* return random number uniformly distributed in range [Minimum, Maximum] */float rand_uniform(float Minimum, float Maximum){	static int init = 0 ;	float TheNum ;	if( !init ) {		/* initialize seed if necessary */		rand_init() ;		init = 1 ;	}/*	TheNum = (float) ((rand() & 0x0FF0 ) >> 4) ; */	return ( (Maximum - Minimum) * rand()  ) + Minimum ; }/* return random number with gaussian distribution ~ N(Mean, Std*Std) */float rand_gaussian(float Mean, float Std){	static short i ;	float unif1, unif2 ;	float gauss ;	unif1 = rand_uniform(0.00001, 1.0) ;  /* generate pair of U[0, 1] */	unif2 = rand_uniform(0.00001, 1.0) ;	gauss = i ?  Mean + Std*sqrt(-2.0*log(unif1))*cos(2*M_PI*unif2) :		     Mean + Std*sqrt(-2.0*log(unif1))*sin(2*M_PI*unif2) ;	i = i ? 0 : 1 ;		/* flip between sin and cos */	return gauss ;}/* return a pair of uncorrelated gaussian numbers */void rand_gausspair(float Mean, float Std, float *G1, float *G2){	float unif1, unif2 ;	unif1 = rand_uniform(0.00001, 1.0) ;  /* generate pair of U[0, 1] */	unif2 = rand_uniform(0.00001, 1.0) ;	*G1 = Mean + Std*sqrt(-2.0*log(unif1))*cos(2*M_PI*unif2) ;	*G2 = Mean + Std*sqrt(-2.0*log(unif1))*sin(2*M_PI*unif2) ;}/* Spetral density of function random() provided by the system is not   white. To minimize its correlation effect, try to space out its sample.   The spacing constant is also generated by random() */void rand_whitened(){	short space ;	space = (short) rand_uniform(0.0, 50.0) ;	if( space == 5 )	/* sometimes we initialize the seed */		rand_init() ;	else		for( ; space >= 0 ; rand(), space-- ) ;}/* throw an gamma coin : p(head) = gamma, p(tail) = 1 - gamma */int rand_gammacoin(float gamma){	return (rand_uniform(0.0, 1.0) <= gamma) ? 1 : 0 ;}/* randomly choose a number from MinChoice to MaxChoice */int rand_choice(int MinChoice, int MaxChoice){	return (int) ( 0.5 + 		rand_uniform((float) MinChoice-0.5, (float) MaxChoice+0.5) ) ;}/* digital implementation of Gaussian distribution */double rand_Pz(double low_z, double high_z) 		/* prob. that a value will fall between the interval,			   assuming mean = 0 and variance = 1 */{	static int firsttime = 1 ;	static double bin[40];	int high, low ;		if(firsttime) {				bin[0] = 0, bin[1]=0.0398, bin[2]=0.0793, bin[3]=0.1179,		bin[4]=0.1554, bin[5]=0.1915, bin[6]=0.2258, bin[7]=0.2580,		bin[8]=0.2881, bin[9]=0.3159, bin[10]=0.3413, bin[11]=0.3643,		bin[12]=0.3849, bin[13]=0.4032, bin[14]=0.4192, bin[15]=0.4332,		bin[16]=0.4452, bin[17]=0.4554, bin[18]=0.4641, bin[19]=0.4713,		bin[20]=0.4772, bin[21]=0.4821, bin[22]=0.4861, bin[23]=0.4893,		bin[24]=0.4918, bin[25]=0.4938, bin[26]=0.4953, bin[27]=0.4965,		bin[28]=0.4974, bin[29]=0.4981, bin[30]=0.4987, bin[31]=0.4990,		bin[32]=0.4993, bin[33]=0.4995, bin[34]=0.4997, bin[35]=0.4998,		bin[36]=0.4998, bin[37]=0.4999, bin[38]=0.4999, bin[39]=0.5000;						firsttime = 0 ;	}		high = (int) CAP(1, high_z*10, 39) ;	low =  (int) CAP(0, low_z*10, 38) ;			return ( (low==0)? 2*(bin[high] - bin[low]) : (bin[high] - bin[low]) );}	int rand_timediff(int index){	static struct tm tp1, tp2 ;//	struct timezone tzp ;		if( index == 1) {		_getsystime(&tp1) ;		return 0 ;	}	else {		_getsystime(&tp2) ;		return (int) (tp2.tm_sec - tp1.tm_sec) ;	}}

⌨️ 快捷键说明

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