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" |
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 { |
|
46 int *F77_FCN (dgennor) (double*, double*, double*); |
|
47 int *F77_FCN (dgenunf) (double*, double*, double*); |
|
48 int *F77_FCN (setall) (int*, int*); |
|
49 int *F77_FCN (getsd) (int*, int*); |
|
50 } |
|
51 |
|
52 static double |
|
53 curr_rand_seed (void) |
|
54 { |
|
55 union d2i { double d; int i[2]; }; |
|
56 union d2i u; |
|
57 F77_FCN (getsd) (&(u.i[0]), &(u.i[1])); |
|
58 return u.d; |
|
59 } |
|
60 |
|
61 static int |
|
62 force_to_fit_range (int i, int lo, int hi) |
|
63 { |
|
64 assert (hi > lo && lo >= 0 && hi > lo); |
|
65 |
|
66 i = i > 0 ? i : -i; |
|
67 |
|
68 if (i < lo) |
|
69 i = lo; |
|
70 else if (i > hi) |
|
71 i = i % hi; |
|
72 |
|
73 return i; |
|
74 } |
|
75 |
|
76 static void |
|
77 set_rand_seed (double val) |
|
78 { |
|
79 union d2i { double d; int i[2]; }; |
|
80 union d2i u; |
|
81 u.d = val; |
|
82 int i0 = force_to_fit_range (u.i[0], 1, 2147483563); |
|
83 int i1 = force_to_fit_range (u.i[1], 1, 2147483399); |
|
84 F77_FCN (setall) (&i0, &i1); |
|
85 } |
|
86 |
|
87 static char * |
|
88 curr_rand_dist (void) |
|
89 { |
|
90 if (current_distribution == uniform) |
|
91 return "uniform"; |
|
92 else if (current_distribution == normal) |
|
93 return "normal"; |
|
94 else |
|
95 { |
|
96 panic_impossible (); |
519
|
97 return 0; |
1
|
98 } |
|
99 } |
|
100 |
701
|
101 DEFUN_DLD_BUILTIN ("rand", Frand, Srand, 2, 1, |
519
|
102 "rand -- generate a random value\n\ |
|
103 \n\ |
|
104 rand (N) -- generate N x N matrix\n\ |
|
105 rand (A) -- generate matrix the size of A\n\ |
|
106 rand (N, M) -- generate N x M matrix\n\ |
|
107 rand (\"dist\") -- get current distribution\n\ |
|
108 rand (DISTRIBUTION) -- set distribution type (\"normal\" or \"uniform\"\n\ |
|
109 rand (SEED) -- get current seed\n\ |
|
110 rand (SEED, N) -- set seed") |
1
|
111 { |
497
|
112 Octave_object retval; |
1
|
113 |
506
|
114 int nargin = args.length (); |
|
115 |
712
|
116 if (nargin > 2 || nargout > 1) |
519
|
117 { |
|
118 print_usage ("rand"); |
|
119 return retval; |
|
120 } |
|
121 |
1
|
122 static int initialized = 0; |
|
123 if (! initialized) |
|
124 { |
|
125 // Make the random number generator give us a different sequence every |
|
126 // time we start octave unless we specifically set the seed. |
|
127 #if 0 |
|
128 int s0 = 1234567890; |
|
129 int s1 = 123456789; |
|
130 #else |
|
131 time_t now; |
|
132 struct tm *tm; |
|
133 |
|
134 time (&now); |
|
135 tm = localtime (&now); |
|
136 |
|
137 int s0 = tm->tm_min * 60 + tm->tm_sec; |
|
138 int s1 = (tm->tm_mday - 1) * 24 * 3600 + tm->tm_hour * 3600 + s0; |
|
139 #endif |
|
140 s0 = force_to_fit_range (s0, 1, 2147483563); |
|
141 s1 = force_to_fit_range (s1, 1, 2147483399); |
|
142 |
|
143 F77_FCN (setall) (&s0, &s1); |
|
144 initialized = 1; |
|
145 } |
|
146 |
|
147 int n = 0; |
|
148 int m = 0; |
712
|
149 if (nargin == 0) |
1
|
150 { |
|
151 n = 1; |
|
152 m = 1; |
|
153 goto gen_matrix; |
|
154 } |
712
|
155 else if (nargin == 1) |
1
|
156 { |
712
|
157 tree_constant tmp = args(0); |
620
|
158 |
|
159 if (tmp.is_string ()) |
1
|
160 { |
620
|
161 char *s_arg = tmp.string_value (); |
1
|
162 if (strcmp (s_arg, "dist") == 0) |
|
163 { |
|
164 char *s = curr_rand_dist (); |
516
|
165 retval(0) = s; |
1
|
166 } |
|
167 else if (strcmp (s_arg, "seed") == 0) |
|
168 { |
|
169 double d = curr_rand_seed (); |
516
|
170 retval(0) = d; |
1
|
171 } |
|
172 else if (strcmp (s_arg, "uniform") == 0) |
|
173 current_distribution = uniform; |
|
174 else if (strcmp (s_arg, "normal") == 0) |
|
175 current_distribution = normal; |
|
176 else |
497
|
177 error ("rand: unrecognized string argument"); |
620
|
178 } |
|
179 else if (tmp.is_scalar_type ()) |
|
180 { |
636
|
181 m = n = NINT (tmp.double_value ()); |
|
182 |
|
183 if (! error_state) |
|
184 goto gen_matrix; |
620
|
185 } |
|
186 else if (tmp.is_range ()) |
|
187 { |
|
188 Range r = tmp.range_value (); |
|
189 n = 1; |
|
190 m = NINT (r.nelem ()); |
1
|
191 goto gen_matrix; |
620
|
192 } |
|
193 else if (tmp.is_matrix_type ()) |
|
194 { |
712
|
195 n = NINT (args(0).rows ()); |
|
196 m = NINT (args(0).columns ()); |
1
|
197 goto gen_matrix; |
620
|
198 } |
|
199 else |
|
200 { |
|
201 gripe_wrong_type_arg ("rand", tmp); |
|
202 return retval; |
1
|
203 } |
|
204 } |
712
|
205 else if (nargin == 2) |
1
|
206 { |
712
|
207 if (args(0).is_string () |
|
208 && strcmp (args(0).string_value (), "seed") == 0) |
1
|
209 { |
712
|
210 double d = args(1).double_value (); |
636
|
211 |
|
212 if (! error_state) |
|
213 set_rand_seed (d); |
1
|
214 } |
|
215 else |
|
216 { |
712
|
217 n = NINT (args(0).double_value ()); |
636
|
218 |
|
219 if (! error_state) |
|
220 { |
712
|
221 m = NINT (args(1).double_value ()); |
636
|
222 |
|
223 if (! error_state) |
|
224 goto gen_matrix; |
|
225 } |
1
|
226 } |
|
227 } |
|
228 |
|
229 return retval; |
|
230 |
|
231 gen_matrix: |
|
232 |
|
233 if (n == 0 || m == 0) |
|
234 { |
620
|
235 Matrix m; |
|
236 retval.resize (1, m); |
1
|
237 } |
|
238 else if (n > 0 && m > 0) |
|
239 { |
|
240 Matrix rand_mat (n, m); |
|
241 for (int j = 0; j < m; j++) |
|
242 for (int i = 0; i < n; i++) |
|
243 { |
|
244 double d_zero = 0.0; |
|
245 double d_one = 1.0; |
|
246 double val; |
|
247 switch (current_distribution) |
|
248 { |
|
249 case uniform: |
|
250 F77_FCN (dgenunf) (&d_zero, &d_one, &val); |
|
251 rand_mat.elem (i, j) = val; |
|
252 break; |
777
|
253 |
1
|
254 case normal: |
|
255 F77_FCN (dgennor) (&d_zero, &d_one, &val); |
|
256 rand_mat.elem (i, j) = val; |
|
257 break; |
777
|
258 |
1
|
259 default: |
|
260 panic_impossible (); |
|
261 break; |
|
262 } |
|
263 } |
|
264 |
516
|
265 retval(0) = rand_mat; |
1
|
266 } |
|
267 else |
216
|
268 error ("rand: invalid negative argument"); |
1
|
269 |
|
270 return retval; |
|
271 } |
|
272 |
|
273 /* |
|
274 ;;; Local Variables: *** |
|
275 ;;; mode: C++ *** |
|
276 ;;; page-delimiter: "^/\\*" *** |
|
277 ;;; End: *** |
|
278 */ |