root/lib/math/rnd.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rand_set_seed
  2. rand_get_seed
  3. rand

   1 #define LONG_MAX     2147483647L
   2 
   3 //-------------------------------------------------------------------
   4 static long int quotient  = LONG_MAX / 16807L;
   5 static long int remainder = LONG_MAX % 16807L;
   6 
   7 static long int seed_val = 1L;
   8 
   9 //-------------------------------------------------------------------
  10 long rand_set_seed(long int sd) {
  11     return seed_val = sd;
  12 }
  13 
  14 //-------------------------------------------------------------------
  15 long rand_get_seed() {
  16     return seed_val;
  17 }
  18 
  19 //-------------------------------------------------------------------
  20 unsigned long int rand() {
  21     if ( seed_val <= quotient )
  22         seed_val = (seed_val * 16807L) % LONG_MAX;
  23     else {
  24         long int high_part = seed_val / quotient;
  25         long int low_part  = seed_val % quotient;
  26 
  27         long int test = 16807L * low_part - remainder * high_part;
  28 
  29         if ( test > 0 )
  30             seed_val = test;
  31         else
  32             seed_val = test + LONG_MAX;
  33     }
  34 
  35     return seed_val;
  36 }
  37 
  38 //-------------------------------------------------------------------

/* [<][>][^][v][top][bottom][index][help] */