7
|
1 // f-rand.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
497
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
519
|
28 #include <time.h> |
|
29 |
1
|
30 #include "tree-const.h" |
|
31 #include "f77-uscore.h" |
|
32 #include "error.h" |
|
33 #include "utils.h" |
544
|
34 #include "help.h" |
519
|
35 #include "defun-dld.h" |
1
|
36 |
|
37 // Possible distributions of random numbers. |
|
38 enum rand_dist { uniform, normal }; |
|
39 |
|
40 // Current distribution of random numbers. |
|
41 static rand_dist current_distribution = uniform; |
|
42 |
|
43 extern "C" |
|
44 { |
|
45 int *F77_FCN (dgennor) (double*, double*, double*); |
|
46 int *F77_FCN (dgenunf) (double*, double*, double*); |
|
47 int *F77_FCN (setall) (int*, int*); |
|
48 int *F77_FCN (getsd) (int*, int*); |
|
49 } |
|
50 |
|
51 static double |
|
52 curr_rand_seed (void) |
|
53 { |
|
54 union d2i { double d; int i[2]; }; |
|
55 union d2i u; |
|
56 F77_FCN (getsd) (&(u.i[0]), &(u.i[1])); |
|
57 return u.d; |
|
58 } |
|
59 |
|
60 static int |
|
61 force_to_fit_range (int i, int lo, int hi) |
|
62 { |
|
63 assert (hi > lo && lo >= 0 && hi > lo); |
|
64 |
|
65 i = i > 0 ? i : -i; |
|
66 |
|
67 if (i < lo) |
|
68 i = lo; |
|
69 else if (i > hi) |
|
70 i = i % hi; |
|
71 |
|
72 return i; |
|
73 } |
|
74 |
|
75 static void |
|
76 set_rand_seed (double val) |
|
77 { |
|
78 union d2i { double d; int i[2]; }; |
|
79 union d2i u; |
|
80 u.d = val; |
|
81 int i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
82 int i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
|
83 F77_FCN (setall) (&i0, &i1); |
|
84 } |
|
85 |
|
86 static char * |
|
87 curr_rand_dist (void) |
|
88 { |
|
89 if (current_distribution == uniform) |
|
90 return "uniform"; |
|
91 else if (current_distribution == normal) |
|
92 return "normal"; |
|
93 else |
|
94 { |
|
95 panic_impossible (); |
519
|
96 return 0; |
1
|
97 } |
|
98 } |
|
99 |
519
|
100 DEFUN_DLD ("rand", Frand, Srand, 2, 1, |
|
101 "rand -- generate a random value\n\ |
|
102 \n\ |
|
103 rand (N) -- generate N x N matrix\n\ |
|
104 rand (A) -- generate matrix the size of A\n\ |
|
105 rand (N, M) -- generate N x M matrix\n\ |
|
106 rand (\"dist\") -- get current distribution\n\ |
|
107 rand (DISTRIBUTION) -- set distribution type (\"normal\" or \"uniform\"\n\ |
|
108 rand (SEED) -- get current seed\n\ |
|
109 rand (SEED, N) -- set seed") |
1
|
110 { |
497
|
111 Octave_object retval; |
1
|
112 |
506
|
113 int nargin = args.length (); |
|
114 |
519
|
115 if (nargin > 3 || nargout > 1) |
|
116 { |
|
117 print_usage ("rand"); |
|
118 return retval; |
|
119 } |
|
120 |
1
|
121 static int initialized = 0; |
|
122 if (! initialized) |
|
123 { |
|
124 // Make the random number generator give us a different sequence every |
|
125 // time we start octave unless we specifically set the seed. |
|
126 #if 0 |
|
127 int s0 = 1234567890; |
|
128 int s1 = 123456789; |
|
129 #else |
|
130 time_t now; |
|
131 struct tm *tm; |
|
132 |
|
133 time (&now); |
|
134 tm = localtime (&now); |
|
135 |
|
136 int s0 = tm->tm_min * 60 + tm->tm_sec; |
|
137 int s1 = (tm->tm_mday - 1) * 24 * 3600 + tm->tm_hour * 3600 + s0; |
|
138 #endif |
|
139 s0 = force_to_fit_range (s0, 1, 2147483563); |
|
140 s1 = force_to_fit_range (s1, 1, 2147483399); |
|
141 |
|
142 F77_FCN (setall) (&s0, &s1); |
|
143 initialized = 1; |
|
144 } |
|
145 |
|
146 int n = 0; |
|
147 int m = 0; |
|
148 if (nargin == 1) |
|
149 { |
|
150 n = 1; |
|
151 m = 1; |
|
152 goto gen_matrix; |
|
153 } |
|
154 else if (nargin == 2) |
|
155 { |
620
|
156 tree_constant tmp = args(1); |
|
157 |
|
158 if (tmp.is_string ()) |
1
|
159 { |
620
|
160 char *s_arg = tmp.string_value (); |
1
|
161 if (strcmp (s_arg, "dist") == 0) |
|
162 { |
|
163 char *s = curr_rand_dist (); |
516
|
164 retval(0) = s; |
1
|
165 } |
|
166 else if (strcmp (s_arg, "seed") == 0) |
|
167 { |
|
168 double d = curr_rand_seed (); |
516
|
169 retval(0) = d; |
1
|
170 } |
|
171 else if (strcmp (s_arg, "uniform") == 0) |
|
172 current_distribution = uniform; |
|
173 else if (strcmp (s_arg, "normal") == 0) |
|
174 current_distribution = normal; |
|
175 else |
497
|
176 error ("rand: unrecognized string argument"); |
620
|
177 } |
|
178 else if (tmp.is_scalar_type ()) |
|
179 { |
|
180 n = NINT (tmp.double_value ()); |
1
|
181 m = n; |
|
182 goto gen_matrix; |
620
|
183 } |
|
184 else if (tmp.is_range ()) |
|
185 { |
|
186 Range r = tmp.range_value (); |
|
187 n = 1; |
|
188 m = NINT (r.nelem ()); |
1
|
189 goto gen_matrix; |
620
|
190 } |
|
191 else if (tmp.is_matrix_type ()) |
|
192 { |
497
|
193 n = NINT (args(1).rows ()); |
|
194 m = NINT (args(1).columns ()); |
1
|
195 goto gen_matrix; |
620
|
196 } |
|
197 else |
|
198 { |
|
199 gripe_wrong_type_arg ("rand", tmp); |
|
200 return retval; |
1
|
201 } |
|
202 } |
|
203 else if (nargin == 3) |
|
204 { |
610
|
205 if (args(1).is_string () |
497
|
206 && strcmp (args(1).string_value (), "seed") == 0) |
1
|
207 { |
628
|
208 double d = args(2).double_value (); |
1
|
209 set_rand_seed (d); |
|
210 } |
|
211 else |
|
212 { |
628
|
213 n = NINT (args(1).double_value ()); |
|
214 m = NINT (args(2).double_value ()); |
1
|
215 goto gen_matrix; |
|
216 } |
|
217 } |
|
218 |
|
219 return retval; |
|
220 |
|
221 gen_matrix: |
|
222 |
|
223 if (n == 0 || m == 0) |
|
224 { |
620
|
225 Matrix m; |
|
226 retval.resize (1, m); |
1
|
227 } |
|
228 else if (n > 0 && m > 0) |
|
229 { |
|
230 Matrix rand_mat (n, m); |
|
231 for (int j = 0; j < m; j++) |
|
232 for (int i = 0; i < n; i++) |
|
233 { |
|
234 double d_zero = 0.0; |
|
235 double d_one = 1.0; |
|
236 double val; |
|
237 switch (current_distribution) |
|
238 { |
|
239 case uniform: |
|
240 F77_FCN (dgenunf) (&d_zero, &d_one, &val); |
|
241 rand_mat.elem (i, j) = val; |
|
242 break; |
|
243 case normal: |
|
244 F77_FCN (dgennor) (&d_zero, &d_one, &val); |
|
245 rand_mat.elem (i, j) = val; |
|
246 break; |
|
247 default: |
|
248 panic_impossible (); |
|
249 break; |
|
250 } |
|
251 } |
|
252 |
516
|
253 retval(0) = rand_mat; |
1
|
254 } |
|
255 else |
216
|
256 error ("rand: invalid negative argument"); |
1
|
257 |
|
258 return retval; |
|
259 } |
|
260 |
|
261 /* |
|
262 ;;; Local Variables: *** |
|
263 ;;; mode: C++ *** |
|
264 ;;; page-delimiter: "^/\\*" *** |
|
265 ;;; End: *** |
|
266 */ |