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