7
|
1 // f-rand.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1993, 1994, 1995 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 |
1192
|
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" |
777
|
33 #include "gripes.h" |
1
|
34 #include "utils.h" |
544
|
35 #include "help.h" |
519
|
36 #include "defun-dld.h" |
1
|
37 |
|
38 // Possible distributions of random numbers. |
|
39 enum rand_dist { uniform, normal }; |
|
40 |
|
41 // Current distribution of random numbers. |
|
42 static rand_dist current_distribution = uniform; |
|
43 |
|
44 extern "C" |
|
45 { |
1255
|
46 int *F77_FCN (dgennor, DGENNOR) (const double&, const double&, |
|
47 double&); |
|
48 |
|
49 int *F77_FCN (dgenunf, DGENUNF) (const double&, const double&, |
|
50 double&); |
|
51 |
|
52 int *F77_FCN (setall, SETALL) (const int&, const int&); |
|
53 |
|
54 int *F77_FCN (getsd, GETSD) (int&, int&); |
1
|
55 } |
|
56 |
|
57 static double |
|
58 curr_rand_seed (void) |
|
59 { |
|
60 union d2i { double d; int i[2]; }; |
|
61 union d2i u; |
1255
|
62 F77_FCN (getsd, GETSD) (u.i[0], u.i[1]); |
1
|
63 return u.d; |
|
64 } |
|
65 |
|
66 static int |
|
67 force_to_fit_range (int i, int lo, int hi) |
|
68 { |
|
69 assert (hi > lo && lo >= 0 && hi > lo); |
|
70 |
|
71 i = i > 0 ? i : -i; |
|
72 |
|
73 if (i < lo) |
|
74 i = lo; |
|
75 else if (i > hi) |
|
76 i = i % hi; |
|
77 |
|
78 return i; |
|
79 } |
|
80 |
|
81 static void |
|
82 set_rand_seed (double val) |
|
83 { |
|
84 union d2i { double d; int i[2]; }; |
|
85 union d2i u; |
|
86 u.d = val; |
|
87 int i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
88 int i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
1255
|
89 F77_FCN (setall, SETALL) (i0, i1); |
1
|
90 } |
|
91 |
|
92 static char * |
|
93 curr_rand_dist (void) |
|
94 { |
|
95 if (current_distribution == uniform) |
|
96 return "uniform"; |
|
97 else if (current_distribution == normal) |
|
98 return "normal"; |
|
99 else |
|
100 { |
|
101 panic_impossible (); |
519
|
102 return 0; |
1
|
103 } |
|
104 } |
|
105 |
701
|
106 DEFUN_DLD_BUILTIN ("rand", Frand, Srand, 2, 1, |
519
|
107 "rand -- generate a random value\n\ |
|
108 \n\ |
|
109 rand (N) -- generate N x N matrix\n\ |
|
110 rand (A) -- generate matrix the size of A\n\ |
|
111 rand (N, M) -- generate N x M matrix\n\ |
|
112 rand (\"dist\") -- get current distribution\n\ |
|
113 rand (DISTRIBUTION) -- set distribution type (\"normal\" or \"uniform\"\n\ |
|
114 rand (SEED) -- get current seed\n\ |
|
115 rand (SEED, N) -- set seed") |
1
|
116 { |
497
|
117 Octave_object retval; |
1
|
118 |
506
|
119 int nargin = args.length (); |
|
120 |
712
|
121 if (nargin > 2 || nargout > 1) |
519
|
122 { |
|
123 print_usage ("rand"); |
|
124 return retval; |
|
125 } |
|
126 |
1
|
127 static int initialized = 0; |
|
128 if (! initialized) |
|
129 { |
|
130 // Make the random number generator give us a different sequence every |
1050
|
131 // time we start octave unless we specifically set the seed. The |
|
132 // technique used below will cycle monthly, but it it does seem to |
|
133 // work ok to give fairly different seeds each time Octave starts. |
|
134 |
1
|
135 #if 0 |
|
136 int s0 = 1234567890; |
|
137 int s1 = 123456789; |
|
138 #else |
|
139 time_t now; |
|
140 struct tm *tm; |
|
141 |
|
142 time (&now); |
|
143 tm = localtime (&now); |
|
144 |
1050
|
145 int hour = tm->tm_hour + 1; |
|
146 int minute = tm->tm_min + 1; |
|
147 int second = tm->tm_sec + 1; |
|
148 |
|
149 int s0 = tm->tm_mday * hour * minute * second; |
|
150 int s1 = hour * minute * second; |
1
|
151 #endif |
|
152 s0 = force_to_fit_range (s0, 1, 2147483563); |
|
153 s1 = force_to_fit_range (s1, 1, 2147483399); |
|
154 |
1255
|
155 F77_FCN (setall, SETALL) (s0, s1); |
1
|
156 initialized = 1; |
|
157 } |
|
158 |
|
159 int n = 0; |
|
160 int m = 0; |
712
|
161 if (nargin == 0) |
1
|
162 { |
|
163 n = 1; |
|
164 m = 1; |
|
165 goto gen_matrix; |
|
166 } |
712
|
167 else if (nargin == 1) |
1
|
168 { |
712
|
169 tree_constant tmp = args(0); |
620
|
170 |
|
171 if (tmp.is_string ()) |
1
|
172 { |
620
|
173 char *s_arg = tmp.string_value (); |
1
|
174 if (strcmp (s_arg, "dist") == 0) |
|
175 { |
|
176 char *s = curr_rand_dist (); |
516
|
177 retval(0) = s; |
1
|
178 } |
|
179 else if (strcmp (s_arg, "seed") == 0) |
|
180 { |
|
181 double d = curr_rand_seed (); |
516
|
182 retval(0) = d; |
1
|
183 } |
|
184 else if (strcmp (s_arg, "uniform") == 0) |
|
185 current_distribution = uniform; |
|
186 else if (strcmp (s_arg, "normal") == 0) |
|
187 current_distribution = normal; |
|
188 else |
497
|
189 error ("rand: unrecognized string argument"); |
620
|
190 } |
|
191 else if (tmp.is_scalar_type ()) |
|
192 { |
1086
|
193 double dval = tmp.double_value (); |
636
|
194 |
1086
|
195 if (xisnan (dval)) |
|
196 { |
|
197 error ("rand: NaN is invalid a matrix dimension"); |
|
198 } |
|
199 else |
|
200 { |
|
201 m = n = NINT (tmp.double_value ()); |
|
202 |
|
203 if (! error_state) |
|
204 goto gen_matrix; |
|
205 } |
620
|
206 } |
|
207 else if (tmp.is_range ()) |
|
208 { |
|
209 Range r = tmp.range_value (); |
|
210 n = 1; |
1086
|
211 m = r.nelem (); |
1
|
212 goto gen_matrix; |
620
|
213 } |
|
214 else if (tmp.is_matrix_type ()) |
|
215 { |
1086
|
216 n = args(0).rows (); |
|
217 m = args(0).columns (); |
1
|
218 goto gen_matrix; |
620
|
219 } |
|
220 else |
|
221 { |
|
222 gripe_wrong_type_arg ("rand", tmp); |
|
223 return retval; |
1
|
224 } |
|
225 } |
712
|
226 else if (nargin == 2) |
1
|
227 { |
712
|
228 if (args(0).is_string () |
|
229 && strcmp (args(0).string_value (), "seed") == 0) |
1
|
230 { |
712
|
231 double d = args(1).double_value (); |
636
|
232 |
|
233 if (! error_state) |
|
234 set_rand_seed (d); |
1
|
235 } |
|
236 else |
|
237 { |
1086
|
238 double dval = args(0).double_value (); |
636
|
239 |
1086
|
240 if (xisnan (dval)) |
636
|
241 { |
1086
|
242 error ("rand: NaN is invalid as a matrix dimension"); |
|
243 } |
|
244 else |
|
245 { |
|
246 n = NINT (dval); |
636
|
247 |
|
248 if (! error_state) |
1086
|
249 { |
|
250 m = NINT (args(1).double_value ()); |
|
251 |
|
252 if (! error_state) |
|
253 goto gen_matrix; |
|
254 } |
636
|
255 } |
1
|
256 } |
|
257 } |
|
258 |
|
259 return retval; |
|
260 |
|
261 gen_matrix: |
|
262 |
|
263 if (n == 0 || m == 0) |
|
264 { |
620
|
265 Matrix m; |
|
266 retval.resize (1, m); |
1
|
267 } |
|
268 else if (n > 0 && m > 0) |
|
269 { |
|
270 Matrix rand_mat (n, m); |
|
271 for (int j = 0; j < m; j++) |
|
272 for (int i = 0; i < n; i++) |
|
273 { |
|
274 double val; |
|
275 switch (current_distribution) |
|
276 { |
|
277 case uniform: |
1255
|
278 F77_FCN (dgenunf, DGENUNF) (0.0, 1.0, val); |
1
|
279 rand_mat.elem (i, j) = val; |
|
280 break; |
777
|
281 |
1
|
282 case normal: |
1255
|
283 F77_FCN (dgennor, DGENNOR) (0.0, 1.0, val); |
1
|
284 rand_mat.elem (i, j) = val; |
|
285 break; |
777
|
286 |
1
|
287 default: |
|
288 panic_impossible (); |
|
289 break; |
|
290 } |
|
291 } |
|
292 |
516
|
293 retval(0) = rand_mat; |
1
|
294 } |
|
295 else |
216
|
296 error ("rand: invalid negative argument"); |
1
|
297 |
|
298 return retval; |
|
299 } |
|
300 |
|
301 /* |
|
302 ;;; Local Variables: *** |
|
303 ;;; mode: C++ *** |
|
304 ;;; page-delimiter: "^/\\*" *** |
|
305 ;;; End: *** |
|
306 */ |