#include "random.h"
#include "log.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

unsigned int random_init()
{
  FILE *f;
  unsigned int z;
  LOG("Initialising pseudo random generator\n");
  if (!(f=fopen(RANDOM_DEV,"r"))) {
    perror(NULL);
    return 1;
  }
  if (!fread(&z, sizeof(unsigned int), 1, f))
    return 1;
  fclose(f);
  LOG("Got seed: %u\n", z);
  srand(z);
  return z;
}

int random_index(int n)
{
  int r;
  /* from de.comp.lang.c FAQ - uses higher bits */
  r = (int)((double)rand() / ((double)RAND_MAX + 1) * n);


  return r;
}
