random.c
Upload User: shhuayu888
Upload Date: 2013-03-28
Package Size: 1788k
Code Size: 5k
Category:

Windows Develop

Development Platform:

Unix_Linux

  1. /****************************************************************************
  2.  File Name  : random.c
  3.  Purpose    : random number generator 
  4.  Release    : Version 1.0
  5.  Date     : Aug 31,1995
  6. GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang Technological
  7. University (NTU), Singapore. 
  8. 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.
  9. 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 :
  10. asschan@ntu.ac.sg
  11. kflai@iti.gov.sg
  12. ***************************************************************************/
  13. #include <sys/time.h>
  14. #include "gsnake.h"
  15. /* use system clock to initialize random number generator */
  16. void rand_init()
  17. {
  18. struct timeval tp ;
  19. struct timezone tzp ;
  20. gettimeofday(&tp, &tzp) ;
  21. srand48(tp.tv_sec) ;
  22. }
  23. /* return random number uniformly distributed in range [Minimum, Maximum] */
  24. float rand_uniform(float Minimum, float Maximum)
  25. {
  26. static int init = 0 ;
  27. float TheNum ;
  28. if( !init ) { /* initialize seed if necessary */
  29. rand_init() ;
  30. init = 1 ;
  31. }
  32. /* TheNum = (float) ((rand() & 0x0FF0 ) >> 4) ; */
  33. return ( (Maximum - Minimum) * drand48()  ) + Minimum ; 
  34. }
  35. /* return random number with gaussian distribution ~ N(Mean, Std*Std) */
  36. float rand_gaussian(float Mean, float Std)
  37. {
  38. static short i ;
  39. float unif1, unif2 ;
  40. float gauss ;
  41. unif1 = rand_uniform(0.00001, 1.0) ;  /* generate pair of U[0, 1] */
  42. unif2 = rand_uniform(0.00001, 1.0) ;
  43. gauss = i ?  Mean + Std*sqrt(-2.0*log(unif1))*cos(2*M_PI*unif2) :
  44.      Mean + Std*sqrt(-2.0*log(unif1))*sin(2*M_PI*unif2) ;
  45. i = i ? 0 : 1 ; /* flip between sin and cos */
  46. return gauss ;
  47. }
  48. /* return a pair of uncorrelated gaussian numbers */
  49. void rand_gausspair(float Mean, float Std, float *G1, float *G2)
  50. {
  51. float unif1, unif2 ;
  52. unif1 = rand_uniform(0.00001, 1.0) ;  /* generate pair of U[0, 1] */
  53. unif2 = rand_uniform(0.00001, 1.0) ;
  54. *G1 = Mean + Std*sqrt(-2.0*log(unif1))*cos(2*M_PI*unif2) ;
  55. *G2 = Mean + Std*sqrt(-2.0*log(unif1))*sin(2*M_PI*unif2) ;
  56. }
  57. /* Spetral density of function random() provided by the system is not
  58.    white. To minimize its correlation effect, try to space out its sample.
  59.    The spacing constant is also generated by random() */
  60. void rand_whitened()
  61. {
  62. short space ;
  63. space = (short) rand_uniform(0.0, 50.0) ;
  64. if( space == 5 ) /* sometimes we initialize the seed */
  65. rand_init() ;
  66. else
  67. for( ; space >= 0 ; rand(), space-- ) ;
  68. }
  69. /* throw an gamma coin : p(head) = gamma, p(tail) = 1 - gamma */
  70. int rand_gammacoin(float gamma)
  71. {
  72. return (rand_uniform(0.0, 1.0) <= gamma) ? 1 : 0 ;
  73. }
  74. /* randomly choose a number from MinChoice to MaxChoice */
  75. int rand_choice(int MinChoice, int MaxChoice)
  76. {
  77. return (int) ( 0.5 + 
  78. rand_uniform((float) MinChoice-0.5, (float) MaxChoice+0.5) ) ;
  79. }
  80. /* digital implementation of Gaussian distribution */
  81. double rand_Pz(double low_z, double high_z) 
  82. /* prob. that a value will fall between the interval,
  83.    assuming mean = 0 and variance = 1 */
  84. {
  85. static int firsttime = 1 ;
  86. static double bin[40];
  87. int high, low ;
  88. if(firsttime) {
  89. bin[0] = 0, bin[1]=0.0398, bin[2]=0.0793, bin[3]=0.1179,
  90. bin[4]=0.1554, bin[5]=0.1915, bin[6]=0.2258, bin[7]=0.2580,
  91. bin[8]=0.2881, bin[9]=0.3159, bin[10]=0.3413, bin[11]=0.3643,
  92. bin[12]=0.3849, bin[13]=0.4032, bin[14]=0.4192, bin[15]=0.4332,
  93. bin[16]=0.4452, bin[17]=0.4554, bin[18]=0.4641, bin[19]=0.4713,
  94. bin[20]=0.4772, bin[21]=0.4821, bin[22]=0.4861, bin[23]=0.4893,
  95. bin[24]=0.4918, bin[25]=0.4938, bin[26]=0.4953, bin[27]=0.4965,
  96. bin[28]=0.4974, bin[29]=0.4981, bin[30]=0.4987, bin[31]=0.4990,
  97. bin[32]=0.4993, bin[33]=0.4995, bin[34]=0.4997, bin[35]=0.4998,
  98. bin[36]=0.4998, bin[37]=0.4999, bin[38]=0.4999, bin[39]=0.5000;
  99. firsttime = 0 ;
  100. }
  101. high = (int) CAP(1, high_z*10, 39) ;
  102. low =  (int) CAP(0, low_z*10, 38) ;
  103. return ( (low==0)? 2*(bin[high] - bin[low]) : (bin[high] - bin[low]) );
  104. }
  105. int rand_timediff(int index)
  106. {
  107. static struct timeval tp1, tp2 ;
  108. struct timezone tzp ;
  109. if( index == 1) {
  110. gettimeofday(&tp1, &tzp) ;
  111. return 0 ;
  112. }
  113. else {
  114. gettimeofday(&tp2, &tzp) ;
  115. return (int) (tp2.tv_sec - tp1.tv_sec) ;
  116. }
  117. }