Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
random.c
Package: gsnake.rar [view]
Upload User: shhuayu888
Upload Date: 2013-03-28
Package Size: 1788k
Code Size: 5k
Category:
Windows Develop
Development Platform:
Unix_Linux
- /****************************************************************************
- File Name : random.c
- Purpose : random number generator
- Release : Version 1.0
- Date : Aug 31,1995
- GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang Technological
- University (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 <sys/time.h>
- #include "gsnake.h"
- /* use system clock to initialize random number generator */
- void rand_init()
- {
- struct timeval tp ;
- struct timezone tzp ;
- gettimeofday(&tp, &tzp) ;
- srand48(tp.tv_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) * drand48() ) + 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 timeval tp1, tp2 ;
- struct timezone tzp ;
- if( index == 1) {
- gettimeofday(&tp1, &tzp) ;
- return 0 ;
- }
- else {
- gettimeofday(&tp2, &tzp) ;
- return (int) (tp2.tv_sec - tp1.tv_sec) ;
- }
- }