/* 
  randomize.c
  
  Copyright (C) 2001 Arthur de Jong
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
*/


#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

#include "randomize.h"


/* the device that is used to seed the rand() function */
#define RANDOMDEV "/dev/urandom"
/* TODO: this shlould probably be moved to configure */


/* initialize the random number generator with a sensible value
   this calls srand() so rand() can be used */
void randomize()
{
  unsigned int seed;
  FILE *fp;
  struct timeval tv;
  
  /* this is probably slow on systems that don't have /dev/urandom */
  if ( (fp=fopen(RANDOMDEV,"r"))==NULL)
  {
    /* seed the random generator with the time */
    gettimeofday(&tv,NULL);
    srand(((unsigned int)(tv.tv_usec))^((unsigned int)getpid()));
  }
  else
  {
    fread(&seed,sizeof(int),1,fp);
    srand(seed);
  }
}

