#import "RanGen.h"
#import <appkit/appkit.h>
#import <math.h>
#import <stdio.h>
#import <sys/time.h>

@implementation RanGen


// ----------------------------------------------------------------
+ new
{
  long t;
  int seed;
  self = [super new];  
  time (&t);
  seed = t; // is time-based!
//  printf("RanGen: new random seed = %d\n",t);
  srandom(seed);
  return self;
}


// ----------------------------------------------------------------
// ran01: Linear congruential method based on
// the random() supplied by the C library.
// Allegedly random() is better than rand().
- (double) ran01 
{
  double aRand;
  aRand = (double)random()/2147483648.0;
  // printf("%lf\n",aRand);
  return aRand;  
} // end of ran01


// ----------------------------------------------------------------
- (int) lowInt: (int) lowestNumber  highInt: (int) highestNumber
{
  double r,t;
  // integer arithmetic requires to add 1 to difference. If this
  // is not done, the highest number is not included.
  // for instance low=10, high=20. Then it has to be r=11, because
  // random is never exactly equal to 1.
  r = (double) highestNumber - (double) lowestNumber + 1;
  t = r * [self ran01];
  return lowestNumber + (int)t;
}
 

@end
