C   48

random c

Guest on 29th June 2022 08:08:47 AM

  1. /* randgen.c => contains random number generator and related utilities
  2.    including advance_random, warmup_random, random, randomize
  3. */
  4. #include <stdio.h>
  5. #include "type.h"
  6.  
  7. #define RANCONST 1.0E-9
  8.  
  9.  
  10. /* GLOBAL VARIABLES */
  11.  
  12. double oldrand[56];     /* array of 55 random numbers */
  13. int    jrand;          /* current random */
  14.  
  15. /* FUNCTIONS */
  16.  
  17. void advance_random()   /* create next batch of 55 random numbers */
  18. {
  19.   int j1;
  20.   double new_random;
  21.  
  22. /*  printf("Entering Advance Random\n");*/
  23.   for (j1=1;j1<=24;j1++){
  24.     new_random = oldrand[j1] - oldrand[j1+31];
  25.     if (new_random < 0.0){
  26.       new_random = new_random + 1.0;
  27.     }
  28.     oldrand[j1] = new_random;
  29.   }
  30.   for (j1=25;j1<=55;j1++){
  31.     new_random = oldrand[j1] - oldrand[j1-24];
  32.     if (new_random < 0.0){
  33.       new_random = new_random + 1.0;
  34.     }
  35.     oldrand[j1] = new_random;
  36.   }
  37. /*  printf("leaving Advance Random\n");*/
  38. }
  39.  
  40. void warmup_random(double random_seed)
  41. {
  42.   int    j1, ii;
  43.   double new_random, prev_random;
  44.  
  45.   oldrand[55] = random_seed;
  46.   new_random  = RANCONST;
  47.   prev_random = random_seed;
  48.   for (j1=1;j1<=54;j1++){
  49.     ii = (21*j1) % 55;
  50.     oldrand[ii] = new_random;
  51.     new_random  = prev_random - new_random;
  52.     if (new_random < 0.0){
  53.       new_random += 1.0;
  54.     }
  55.     prev_random = oldrand[ii];
  56.   }
  57.   advance_random();
  58.   advance_random();
  59.   advance_random();
  60.   jrand = 0;
  61. }
  62.  
  63. double f_random()
  64. {
  65. /* Fetch a single random number between 0.0 and 1.0 - Substractive Method
  66.    See Knuth, D. (1969), v.2 for details */
  67.  
  68.   jrand++;
  69.   if (jrand > 55){
  70.     jrand = 1;
  71.     advance_random();
  72.   }
  73.   return (oldrand[jrand]);
  74. }
  75.  
  76. void randomize(POPULATION *p)  /* Get seed number for random and start it up */
  77. {
  78.   int err;
  79.   err = printf("Enter seed random number (0.0..1.0) > ");
  80.   err = scanf("%lf",&p->randomseed);
  81.   while ( p->randomseed < 0.0 || p->randomseed > 1.0){
  82.     err = printf("ERROR: Enter seed random number again (0.0..1.0) > ");
  83.     err = scanf("%lf\n",&p->randomseed);
  84.   }
  85.  
  86.  
  87.   warmup_random(p->randomseed);
  88. }
  89.  
  90.  
  91. int flip(double prob)
  92. {/* Flip a biased coin...true if heads */
  93.  
  94. if (prob == 1.0)
  95.   return 1;
  96. else
  97.   return (f_random() <= prob);
  98.  
  99. }
  100.  
  101. int rnd(int low, int high)
  102. { /* pick a random int between low and high */
  103.   double fr;
  104.   int i;
  105.   fr = f_random();
  106.  
  107.   if(low >= high) {
  108.     i = low;
  109.   } else {
  110.     i = ((int)(fr * (double)(high - low + 1)) + low);
  111.     if (i > high) i = high;
  112.   }
  113.   return i;
  114. }

Raw Paste


Login or Register to edit or fork this paste. It's free.