File: random.c

package info (click to toggle)
bow 19991122-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,544 kB
  • ctags: 2,987
  • sloc: ansic: 38,660; lisp: 1,072; makefile: 594; perl: 492; yacc: 149; sh: 91
file content (95 lines) | stat: -rw-r--r-- 2,356 bytes parent folder | download
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
/* random.c - pseudo-random number generators for libbow
   Copyright (C) 1998, 1999 Andrew McCallum

   Written by:  Andrew Kachites McCallum <mccallum@cs.cmu.edu>

   This file is part of the Bag-Of-Words Library, `libbow'.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation, version 2.
   
   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */


#include <bow/libbow.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

/* non-zero if the random number generator is already seeded */
static int already_seeded = 0;

/* This function seeds the random number generator if needed.  Call
   before and random number generator usage, instead of srand */
void
bow_random_set_seed ()
{
  if (!already_seeded)
    {
      if (bow_random_seed == -1)
#ifdef HAVE_GETTIMEOFDAY /* for SunOS */
	{
	  struct timeval tv;
	  struct timezone tz;
	  
	  gettimeofday (&tv, &tz);
	  bow_random_seed = tv.tv_usec;
	  srandom (bow_random_seed);
	}
#else
        srandom (time (NULL));
#endif
      else
	srandom (bow_random_seed);

      already_seeded = 1;
    }
  return;
}

/* Set the seed to the same value it had the first time it was set
   during this run. */
void
bow_random_reset_seed ()
{
  assert (bow_random_seed >= 0);
  srandom (bow_random_seed);
}

/* Return an double between low and high, inclusive */
double
bow_random_double (double low, double high)
{
  long r;
  double rd;

  assert (high - low > 0);
  r = random();
#ifdef RAND_MAX /* for SunOS */
  rd = ((double)r) / ((double)RAND_MAX);
#else
  rd = ((double)r) / ((double)2147483647);
#endif  
  rd *= (high - low);
  rd += low;
  return rd;
}

/* Return an double between 0 and 1, exclusive */
double
bow_random_01 ()
{
  double r;
  while ((r = bow_random_double (0.0, 1.0)) <= 0.0 || r >= 1.0)
    ;
  return r;
}