Mercurial > hg > octave-lyh
annotate liboctave/oct-rand.cc @ 11586:12df7854fa7c
strip trailing whitespace from source files
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:24:59 -0500 |
parents | fd0a3ac60b0e |
children | 7dd7cccf0757 |
rev | line source |
---|---|
4308 | 1 /* |
2 | |
11523 | 3 Copyright (C) 2003-2011 John W. Eaton |
4308 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
4308 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4308 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include <config.h> | |
25 #endif | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
26 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
27 #include <map> |
5730 | 28 #include <vector> |
4308 | 29 |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
30 #include <stdint.h> |
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
31 |
4308 | 32 #include "f77-fcn.h" |
5730 | 33 #include "lo-ieee.h" |
4308 | 34 #include "lo-error.h" |
5730 | 35 #include "lo-mappers.h" |
4308 | 36 #include "oct-rand.h" |
4415 | 37 #include "oct-time.h" |
5730 | 38 #include "data-conv.h" |
39 #include "randmtzig.h" | |
40 #include "randpoisson.h" | |
41 #include "randgamma.h" | |
6435 | 42 #include "mach-info.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8124
diff
changeset
|
43 #include "oct-locbuf.h" |
4308 | 44 |
45 extern "C" | |
46 { | |
4552 | 47 F77_RET_T |
48 F77_FUNC (dgennor, DGENNOR) (const double&, const double&, double&); | |
4308 | 49 |
4552 | 50 F77_RET_T |
51 F77_FUNC (dgenunf, DGENUNF) (const double&, const double&, double&); | |
52 | |
53 F77_RET_T | |
5730 | 54 F77_FUNC (dgenexp, DGENEXP) (const double&, double&); |
55 | |
56 F77_RET_T | |
57 F77_FUNC (dignpoi, DIGNPOI) (const double&, double&); | |
58 | |
59 F77_RET_T | |
60 F77_FUNC (dgengam, DGENGAM) (const double&, const double&, double&); | |
61 | |
62 F77_RET_T | |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
63 F77_FUNC (setall, SETALL) (const int32_t&, const int32_t&); |
4308 | 64 |
4552 | 65 F77_RET_T |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
66 F77_FUNC (getsd, GETSD) (int32_t&, int32_t&); |
4308 | 67 |
4552 | 68 F77_RET_T |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
69 F77_FUNC (setsd, SETSD) (const int32_t&, const int32_t&); |
4308 | 70 |
4552 | 71 F77_RET_T |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
72 F77_FUNC (setcgn, SETCGN) (const int32_t&); |
4308 | 73 } |
74 | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
75 octave_rand *octave_rand::instance = 0; |
6326 | 76 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
77 octave_rand::octave_rand (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
78 : current_distribution (uniform_dist), use_old_generators (false), |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
79 rand_states () |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
80 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
81 initialize_ranlib_generators (); |
4308 | 82 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
83 initialize_mersenne_twister (); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
84 } |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
85 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
86 bool |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
87 octave_rand::instance_ok (void) |
4308 | 88 { |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
89 bool retval = true; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
90 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
91 if (! instance) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
92 instance = new octave_rand (); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
93 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
94 if (! instance) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
95 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
96 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
97 ("unable to create octave_rand object!"); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
98 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
99 retval = false; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
100 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
101 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
102 return retval; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
103 } |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
104 |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
105 double |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
106 octave_rand::do_seed (void) |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
107 { |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
108 union d2i { double d; int32_t i[2]; }; |
4308 | 109 union d2i u; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
110 |
6435 | 111 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
112 | |
113 switch (ff) | |
114 { | |
115 case oct_mach_info::flt_fmt_ieee_big_endian: | |
116 F77_FUNC (getsd, GETSD) (u.i[1], u.i[0]); | |
117 break; | |
118 default: | |
119 F77_FUNC (getsd, GETSD) (u.i[0], u.i[1]); | |
120 break; | |
121 } | |
122 | |
4308 | 123 return u.d; |
124 } | |
125 | |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
126 static int32_t |
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
127 force_to_fit_range (int32_t i, int32_t lo, int32_t hi) |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
128 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
129 assert (hi > lo && lo >= 0 && hi > lo); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
130 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
131 i = i > 0 ? i : -i; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
132 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
133 if (i < lo) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
134 i = lo; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
135 else if (i > hi) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
136 i = i % hi; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
137 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
138 return i; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
139 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
140 |
4308 | 141 void |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
142 octave_rand::do_seed (double s) |
4308 | 143 { |
5730 | 144 use_old_generators = true; |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
145 |
6435 | 146 int i0, i1; |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
147 union d2i { double d; int32_t i[2]; }; |
4308 | 148 union d2i u; |
149 u.d = s; | |
6435 | 150 |
151 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); | |
152 | |
153 switch (ff) | |
154 { | |
155 case oct_mach_info::flt_fmt_ieee_big_endian: | |
156 i1 = force_to_fit_range (u.i[0], 1, 2147483563); | |
157 i0 = force_to_fit_range (u.i[1], 1, 2147483399); | |
158 break; | |
159 default: | |
160 i0 = force_to_fit_range (u.i[0], 1, 2147483563); | |
161 i1 = force_to_fit_range (u.i[1], 1, 2147483399); | |
162 break; | |
163 } | |
164 | |
4308 | 165 F77_FUNC (setsd, SETSD) (i0, i1); |
166 } | |
167 | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
168 void |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
169 octave_rand::do_reset (void) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
170 { |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
171 use_old_generators = true; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
172 initialize_ranlib_generators (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
173 } |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
174 |
5730 | 175 ColumnVector |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
176 octave_rand::do_state (const std::string& d) |
5730 | 177 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
178 return rand_states[d.empty () ? current_distribution : get_dist_id (d)]; |
5730 | 179 } |
180 | |
181 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
182 octave_rand::do_state (const ColumnVector& s, const std::string& d) |
5730 | 183 { |
184 use_old_generators = false; | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
185 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
186 int old_dist = current_distribution; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
187 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
188 int new_dist = d.empty () ? current_distribution : get_dist_id (d); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
189 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
190 ColumnVector saved_state; |
5730 | 191 |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
192 if (old_dist != new_dist) |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
193 saved_state = get_internal_state (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
194 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
195 set_internal_state (s); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
196 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
197 rand_states[new_dist] = get_internal_state (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
198 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
199 if (old_dist != new_dist) |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
200 rand_states[old_dist] = saved_state; |
5730 | 201 } |
202 | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
203 void |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
204 octave_rand::do_reset (const std::string& d) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
205 { |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
206 use_old_generators = false; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
207 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
208 int old_dist = current_distribution; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
209 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
210 int new_dist = d.empty () ? current_distribution : get_dist_id (d); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
211 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
212 ColumnVector saved_state; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
213 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
214 if (old_dist != new_dist) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
215 saved_state = get_internal_state (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
216 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
217 oct_init_by_entropy (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
218 rand_states[new_dist] = get_internal_state (); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
219 |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
220 if (old_dist != new_dist) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
221 rand_states[old_dist] = saved_state; |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
222 } |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10350
diff
changeset
|
223 |
4308 | 224 std::string |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
225 octave_rand::do_distribution (void) |
4308 | 226 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
227 std::string retval; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
228 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
229 switch (current_distribution) |
4308 | 230 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
231 case uniform_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
232 retval = "uniform"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
233 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
234 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
235 case normal_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
236 retval = "normal"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
237 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
238 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
239 case expon_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
240 retval = "exponential"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
241 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
242 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
243 case poisson_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
244 retval = "poisson"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
245 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
246 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
247 case gamma_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
248 retval = "gamma"; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
249 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
250 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
251 default: |
7536 | 252 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
253 ("rand: invalid distribution ID = %d", current_distribution); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
254 break; |
4308 | 255 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
256 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
257 return retval; |
4308 | 258 } |
259 | |
260 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
261 octave_rand::do_distribution (const std::string& d) |
4308 | 262 { |
7536 | 263 int id = get_dist_id (d); |
264 | |
265 switch (id) | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
266 { |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
267 case uniform_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
268 octave_rand::uniform_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
269 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
270 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
271 case normal_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
272 octave_rand::normal_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
273 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
274 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
275 case expon_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
276 octave_rand::exponential_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
277 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
278 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
279 case poisson_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
280 octave_rand::poisson_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
281 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
282 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
283 case gamma_dist: |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
284 octave_rand::gamma_distribution (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
285 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
286 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
287 default: |
7536 | 288 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
289 ("rand: invalid distribution ID = %d", id); |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
290 break; |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
291 } |
4308 | 292 } |
293 | |
294 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
295 octave_rand::do_uniform_distribution (void) |
4308 | 296 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
297 switch_to_generator (uniform_dist); |
4308 | 298 |
299 F77_FUNC (setcgn, SETCGN) (uniform_dist); | |
300 } | |
301 | |
302 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
303 octave_rand::do_normal_distribution (void) |
4308 | 304 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
305 switch_to_generator (normal_dist); |
4308 | 306 |
307 F77_FUNC (setcgn, SETCGN) (normal_dist); | |
308 } | |
309 | |
5730 | 310 void |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
311 octave_rand::do_exponential_distribution (void) |
5730 | 312 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
313 switch_to_generator (expon_dist); |
5730 | 314 |
315 F77_FUNC (setcgn, SETCGN) (expon_dist); | |
316 } | |
317 | |
318 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
319 octave_rand::do_poisson_distribution (void) |
5730 | 320 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
321 switch_to_generator (poisson_dist); |
5730 | 322 |
323 F77_FUNC (setcgn, SETCGN) (poisson_dist); | |
324 } | |
325 | |
326 void | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
327 octave_rand::do_gamma_distribution (void) |
5730 | 328 { |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
329 switch_to_generator (gamma_dist); |
5730 | 330 |
331 F77_FUNC (setcgn, SETCGN) (gamma_dist); | |
332 } | |
333 | |
334 | |
4308 | 335 double |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
336 octave_rand::do_scalar (double a) |
4308 | 337 { |
338 double retval = 0.0; | |
339 | |
5730 | 340 if (use_old_generators) |
4308 | 341 { |
5730 | 342 switch (current_distribution) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
343 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
344 case uniform_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
345 F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
346 break; |
4308 | 347 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
348 case normal_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
349 F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
350 break; |
4308 | 351 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
352 case expon_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
353 F77_FUNC (dgenexp, DGENEXP) (1.0, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
354 break; |
4308 | 355 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
356 case poisson_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
357 if (a < 0.0 || xisnan(a) || xisinf(a)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
358 retval = octave_NaN; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
359 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
360 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
361 // workaround bug in ignpoi, by calling with different Mu |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
362 F77_FUNC (dignpoi, DIGNPOI) (a + 1, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
363 F77_FUNC (dignpoi, DIGNPOI) (a, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
364 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
365 break; |
4308 | 366 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
367 case gamma_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
368 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
369 retval = octave_NaN; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
370 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
371 F77_FUNC (dgengam, DGENGAM) (1.0, a, retval); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
372 break; |
4308 | 373 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
374 default: |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
375 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
376 ("rand: invalid distribution ID = %d", current_distribution); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
377 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
378 } |
4308 | 379 } |
380 else | |
4543 | 381 { |
382 switch (current_distribution) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
383 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
384 case uniform_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
385 retval = oct_randu (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
386 break; |
4543 | 387 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
388 case normal_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
389 retval = oct_randn (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
390 break; |
5730 | 391 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
392 case expon_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
393 retval = oct_rande (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
394 break; |
5730 | 395 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
396 case poisson_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
397 retval = oct_randp (a); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
398 break; |
5730 | 399 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
400 case gamma_dist: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
401 retval = oct_randg (a); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
402 break; |
4543 | 403 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
404 default: |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
405 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
406 ("rand: invalid distribution ID = %d", current_distribution); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
407 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
408 } |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
409 |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
410 save_state (); |
4543 | 411 } |
412 | |
413 return retval; | |
414 } | |
415 | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
416 Matrix |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
417 octave_rand::do_matrix (octave_idx_type n, octave_idx_type m, double a) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
418 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
419 Matrix retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
420 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
421 if (n >= 0 && m >= 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
422 { |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
423 retval.clear (n, m); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
424 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
425 if (n > 0 && m > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
426 fill (retval.capacity(), retval.fortran_vec(), a); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
427 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
428 else |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
429 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
430 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
431 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
432 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
433 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
434 NDArray |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
435 octave_rand::do_nd_array (const dim_vector& dims, double a) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
436 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
437 NDArray retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
438 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
439 if (! dims.all_zero ()) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
440 { |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
441 retval.clear (dims); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
442 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
443 fill (retval.capacity(), retval.fortran_vec(), a); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
444 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
445 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
446 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
447 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
448 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
449 Array<double> |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
450 octave_rand::do_vector (octave_idx_type n, double a) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
451 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
452 Array<double> retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
453 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
454 if (n > 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
455 { |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10331
diff
changeset
|
456 retval.clear (n, 1); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
457 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
458 fill (retval.capacity (), retval.fortran_vec (), a); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
459 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
460 else if (n < 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
461 (*current_liboctave_error_handler) ("rand: invalid negative argument"); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
462 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
463 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
464 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
465 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
466 // Make the random number generator give us a different sequence every |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
467 // time we start octave unless we specifically set the seed. The |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
468 // technique used below will cycle monthly, but it it does seem to |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
469 // work ok to give fairly different seeds each time Octave starts. |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
470 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
471 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
472 octave_rand::initialize_ranlib_generators (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
473 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
474 octave_localtime tm; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
475 int stored_distribution = current_distribution; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
476 F77_FUNC (setcgn, SETCGN) (uniform_dist); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
477 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
478 int hour = tm.hour() + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
479 int minute = tm.min() + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
480 int second = tm.sec() + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
481 |
10331
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
482 int32_t s0 = tm.mday() * hour * minute * second; |
6a50aa2cdf00
never compile ranlib with 8-byte integers
John W. Eaton <jwe@octave.org>
parents:
10314
diff
changeset
|
483 int32_t s1 = hour * minute * second; |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
484 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
485 s0 = force_to_fit_range (s0, 1, 2147483563); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
486 s1 = force_to_fit_range (s1, 1, 2147483399); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
487 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
488 F77_FUNC (setall, SETALL) (s0, s1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
489 F77_FUNC (setcgn, SETCGN) (stored_distribution); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
490 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
491 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
492 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
493 octave_rand::initialize_mersenne_twister (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
494 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
495 oct_init_by_entropy (); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
496 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
497 ColumnVector s = get_internal_state (); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
498 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
499 rand_states[uniform_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
500 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
501 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
502 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
503 rand_states[normal_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
504 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
505 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
506 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
507 rand_states[expon_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
508 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
509 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
510 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
511 rand_states[poisson_dist] = s; |
8124
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
512 |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
513 oct_init_by_entropy (); |
d227d096d49e
oct-rand.cc (initialize_mersenne_twister): use separate initializations for each generator
Brian Gough
parents:
7537
diff
changeset
|
514 s = get_internal_state (); |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
515 rand_states[gamma_dist] = s; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
516 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
517 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
518 ColumnVector |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
519 octave_rand::get_internal_state (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
520 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
521 ColumnVector s (MT_N + 1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
522 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
523 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
524 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
525 oct_get_state (tmp); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
526 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
527 for (octave_idx_type i = 0; i <= MT_N; i++) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
528 s.elem (i) = static_cast<double> (tmp [i]); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
529 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
530 return s; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
531 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
532 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
533 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
534 octave_rand::save_state (void) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
535 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
536 rand_states[current_distribution] = get_internal_state ();; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
537 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
538 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
539 int |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
540 octave_rand::get_dist_id (const std::string& d) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
541 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
542 int retval = unknown_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
543 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
544 if (d == "uniform" || d == "rand") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
545 retval = uniform_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
546 else if (d == "normal" || d == "randn") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
547 retval = normal_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
548 else if (d == "exponential" || d == "rande") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
549 retval = expon_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
550 else if (d == "poisson" || d == "randp") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
551 retval = poisson_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
552 else if (d == "gamma" || d == "randg") |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
553 retval = gamma_dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
554 else |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
555 (*current_liboctave_error_handler) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
556 ("rand: invalid distribution `%s'", d.c_str ()); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
557 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
558 return retval; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
559 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
560 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
561 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
562 octave_rand::set_internal_state (const ColumnVector& s) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
563 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
564 octave_idx_type len = s.length (); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
565 octave_idx_type n = len < MT_N + 1 ? len : MT_N + 1; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
566 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
567 OCTAVE_LOCAL_BUFFER (uint32_t, tmp, MT_N + 1); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
568 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
569 for (octave_idx_type i = 0; i < n; i++) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
570 tmp[i] = static_cast<uint32_t> (s.elem(i)); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
571 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
572 if (len == MT_N + 1 && tmp[MT_N] <= MT_N && tmp[MT_N] > 0) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
573 oct_set_state (tmp); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
574 else |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
575 oct_init_by_array (tmp, len); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
576 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
577 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
578 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
579 octave_rand::switch_to_generator (int dist) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
580 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
581 if (dist != current_distribution) |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
582 { |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
583 current_distribution = dist; |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
584 |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
585 set_internal_state (rand_states[dist]); |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
586 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
587 } |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
588 |
5730 | 589 #define MAKE_RAND(len) \ |
4308 | 590 do \ |
591 { \ | |
592 double val; \ | |
5730 | 593 for (volatile octave_idx_type i = 0; i < len; i++) \ |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
594 { \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
595 octave_quit (); \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
596 RAND_FUNC (val); \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
597 v[i] = val; \ |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
598 } \ |
4308 | 599 } \ |
600 while (0) | |
601 | |
7537
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
602 void |
a2950622f070
make octave_rand a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7536
diff
changeset
|
603 octave_rand::fill (octave_idx_type len, double *v, double a) |
5730 | 604 { |
605 if (len < 1) | |
606 return; | |
607 | |
608 switch (current_distribution) | |
609 { | |
610 case uniform_dist: | |
611 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
612 { |
5730 | 613 #define RAND_FUNC(x) F77_FUNC (dgenunf, DGENUNF) (0.0, 1.0, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
614 MAKE_RAND (len); |
5730 | 615 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
616 } |
5730 | 617 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
618 oct_fill_randu (len, v); |
5730 | 619 break; |
620 | |
621 case normal_dist: | |
622 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
623 { |
5730 | 624 #define RAND_FUNC(x) F77_FUNC (dgennor, DGENNOR) (0.0, 1.0, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
625 MAKE_RAND (len); |
5730 | 626 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
627 } |
5730 | 628 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
629 oct_fill_randn (len, v); |
5730 | 630 break; |
631 | |
632 case expon_dist: | |
633 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
634 { |
5730 | 635 #define RAND_FUNC(x) F77_FUNC (dgenexp, DGENEXP) (1.0, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
636 MAKE_RAND (len); |
5730 | 637 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
638 } |
5730 | 639 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
640 oct_fill_rande (len, v); |
5730 | 641 break; |
642 | |
643 case poisson_dist: | |
644 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
645 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
646 if (a < 0.0 || xisnan(a) || xisinf(a)) |
5730 | 647 #define RAND_FUNC(x) x = octave_NaN; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
648 MAKE_RAND (len); |
5730 | 649 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
650 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
651 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
652 // workaround bug in ignpoi, by calling with different Mu |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
653 double tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
654 F77_FUNC (dignpoi, DIGNPOI) (a + 1, tmp); |
5730 | 655 #define RAND_FUNC(x) F77_FUNC (dignpoi, DIGNPOI) (a, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
656 MAKE_RAND (len); |
5730 | 657 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
658 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
659 } |
5730 | 660 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
661 oct_fill_randp (a, len, v); |
5730 | 662 break; |
663 | |
664 case gamma_dist: | |
665 if (use_old_generators) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
666 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
667 if (a <= 0.0 || xisnan(a) || xisinf(a)) |
5730 | 668 #define RAND_FUNC(x) x = octave_NaN; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
669 MAKE_RAND (len); |
5730 | 670 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
671 else |
5730 | 672 #define RAND_FUNC(x) F77_FUNC (dgengam, DGENGAM) (1.0, a, x) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
673 MAKE_RAND (len); |
5730 | 674 #undef RAND_FUNC |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
675 } |
5730 | 676 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
677 oct_fill_randg (a, len, v); |
5730 | 678 break; |
679 | |
680 default: | |
7536 | 681 (*current_liboctave_error_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
682 ("rand: invalid distribution ID = %d", current_distribution); |
5730 | 683 break; |
684 } | |
685 | |
7533
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
686 save_state (); |
ff52243af934
save state separately for each MT random number generator
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
687 |
5730 | 688 return; |
689 } |