1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 /* Deal with broken system random number generator. */
30 #define real_rand rand
31 #define real_srand srand
32 #define REAL_RAND_MAX RAND_MAX
33 #else /* !HAVE_GOOD_RANDOM */
34 #define REAL_RAND_MAX 32767
36 /* Some systems are so broken that they do not supply a value for
37 RAND_MAX. There is absolutely no reliable way to determine this
38 value, either. So we must supply our own. This one is the one
39 presented in the ANSI C standard as strictly compliant. */
40 static unsigned long int next = 1;
45 next = next * 1103515245 + 12345;
46 return (unsigned int)(next / 65536) % 32768;
50 real_srand (unsigned int seed)
54 #endif /* !HAVE_GOOD_RANDOM */
56 /* The random number generator here is an implementation in C of
57 Knuth's Algorithm 3.2.2B (Randomizing by Shuffling) in _The Art of
58 Computer Programming_, Vol. 2. */
66 /* Initializes the random number generator. Should be called once by
67 every cmd_*() that uses random numbers. Note that this includes
68 all procedures that use expressions since they may generate random
71 setup_randomize (void)
73 static time_t curtime;
76 if (set_seed == NOT_LONG)
80 real_srand (curtime++);
83 real_srand (set_seed);
87 for (i = 0; i < k; i++)
93 /* Standard shuffling procedure for increasing randomness of the ANSI
94 C random number generator. Returns a random number R where 0 <= R
99 int j = k * Y / RAND_MAX;
105 /* Returns a random number R where 0 <= R <= X. */
107 rand_uniform (double x)
109 return ((double) shuffle ()) / (((double) RAND_MAX) / x);
112 /* Returns a random number from the distribution with mean 0 and
113 standard deviation X. This uses algorithm P in section 3.4.1C of
114 Knuth's _Art of Computer Programming_, Vol 2. */
116 rand_normal (double x)
123 if (X2 != NOT_DOUBLE)
131 U1 = ((double) shuffle ()) / RAND_MAX;
132 U2 = ((double) shuffle ()) / RAND_MAX;
135 S = V1 * V1 + V2 * V2;
138 X1 = V1 * sqrt (-2. * log (S) / S);
139 X2 = V2 * sqrt (-2. * log (S) / S);
143 /* Returns a random integer R, where 0 <= R < X. */
147 return shuffle () % x;