1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
/*
random.c
Copyright (C) 2001, 2002, 2004 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 "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <limits.h>
#include "rl.h"
#include "random.h"
#if defined(USE_RAND_RNG)
/* initialize the random number generator with a sensible value
this calls srand() so rand() can be used */
void
randomize()
{
struct timeval tv;
gettimeofday(&tv,NULL);
srand(((unsigned int)(tv.tv_usec))^((unsigned int)getpid()));
}
/* returns a random number x where 0 <= x < max */
int
random_below(int max)
{
int i;
int j;
i=RAND_MAX/max;
/* there realy should be a better way for this */
/* TODO: fix with something smart so that this even works for large values
(on systems with a low RAND_MAX maybe two calls
to rand() could be used) */
/* TODO: add range checking 0<max<RAND_MAX */
do
{
j=rand()/i;
}
while (j>=max);
return j;
}
#elif defined(USE_DEV_RNG)
#define RANDOM_DEVICE "/dev/urandom"
/* the file pointer that is used to read from the random device */
static FILE *random_fp;
/* open random device for reading */
void
randomize()
{
random_fp=fopen(RANDOM_DEVICE,"rb");
}
/* returns a random number x where 0 <= x < max */
int
random_below(int max)
{
int i;
int j;
int r;
i=INT_MAX/max;
/* TODO: check same problems as above */
do
{
if (fread(&r,sizeof(int),1,random_fp)!=1)
{
fprintf(stderr,_("Error reading from %s\n"),RANDOM_DEVICE);
exit(1);
}
j=(r&INT_MAX)/i;
}
while (j>=max);
return j;
}
#else
#error invalid random number generator selected
#endif
/* thow the dice and return true (1)
with a chance of x/y (false=0) */
int
random_draw(int x,int y)
{
return random_below(y)<x;
}
|