Mercurial > hg > octave-lyh
annotate src/DLD-FUNCTIONS/rand.cc @ 10787:ac433932ce23
Correct typo in rande documentation (bug #30446).
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Wed, 14 Jul 2010 09:57:32 -0700 |
parents | d1f920d1ce0c |
children | 3140cb7a05a1 |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2005, 2006, |
8920 | 4 2007, 2008, 2009 John W. Eaton |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
5 Copyright (C) 2009 VZLU Prague |
2928 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
2928 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
2928 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
29 #include <ctime> | |
30 | |
31 #include <string> | |
32 | |
33 #include "f77-fcn.h" | |
34 #include "lo-mappers.h" | |
4307 | 35 #include "oct-rand.h" |
4153 | 36 #include "quit.h" |
2928 | 37 |
38 #include "defun-dld.h" | |
39 #include "error.h" | |
40 #include "gripes.h" | |
41 #include "oct-obj.h" | |
42 #include "unwind-prot.h" | |
43 #include "utils.h" | |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
44 #include "ov-re-mat.h" |
2928 | 45 |
6437 | 46 /* |
47 %!shared __random_statistical_tests__ | |
48 %! % Flag whether the statistical tests should be run in "make check" or not | |
49 %! __random_statistical_tests__ = 0; | |
50 */ | |
51 | |
4307 | 52 static octave_value |
5730 | 53 do_rand (const octave_value_list& args, int nargin, const char *fcn, |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
54 const std::string& distribution, bool additional_arg = false) |
2928 | 55 { |
4307 | 56 octave_value retval; |
5730 | 57 NDArray a; |
58 int idx = 0; | |
59 dim_vector dims; | |
2928 | 60 |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
61 unwind_protect frame; |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
62 // Restore current distribution on any exit. |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
63 frame.add_fcn (octave_rand::distribution, |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
64 octave_rand::distribution ()); |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
65 |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
66 octave_rand::distribution (distribution); |
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
67 |
5730 | 68 if (additional_arg) |
69 { | |
70 if (nargin == 0) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
71 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
72 error ("%s: expecting at least one argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
73 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
74 } |
5730 | 75 else if (args(0).is_string()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
76 additional_arg = false; |
5730 | 77 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
78 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
79 a = args(0).array_value (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
80 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
81 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
82 error ("%s: expecting scalar or matrix arguments", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
83 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
84 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
85 idx++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
86 nargin--; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
87 } |
5730 | 88 } |
2928 | 89 |
4543 | 90 switch (nargin) |
2928 | 91 { |
4543 | 92 case 0: |
93 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
94 if (additional_arg) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
95 dims = a.dims (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
96 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
97 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
98 dims.resize (2); |
4543 | 99 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
100 dims(0) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
101 dims(1) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
102 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
103 goto gen_matrix; |
4543 | 104 } |
105 break; | |
2928 | 106 |
4543 | 107 case 1: |
108 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
109 octave_value tmp = args(idx); |
4543 | 110 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
111 if (tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
112 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
113 std::string s_arg = tmp.string_value (); |
2928 | 114 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
115 if (s_arg == "dist") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
116 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
117 retval = octave_rand::distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
118 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
119 else if (s_arg == "seed") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
120 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
121 retval = octave_rand::seed (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
122 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
123 else if (s_arg == "state" || s_arg == "twister") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
124 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
125 retval = octave_rand::state (fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
126 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
127 else if (s_arg == "uniform") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
128 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
129 octave_rand::uniform_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
130 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
131 else if (s_arg == "normal") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
132 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
133 octave_rand::normal_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
134 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
135 else if (s_arg == "exponential") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
136 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
137 octave_rand::exponential_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
138 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
139 else if (s_arg == "poisson") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
140 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
141 octave_rand::poisson_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
142 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
143 else if (s_arg == "gamma") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
144 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
145 octave_rand::gamma_distribution (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
146 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
147 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
148 error ("%s: unrecognized string argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
149 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
150 else if (tmp.is_scalar_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
151 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
152 double dval = tmp.double_value (); |
2928 | 153 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
154 if (xisnan (dval)) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
155 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
156 error ("%s: NaN is invalid a matrix dimension", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
157 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
158 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
159 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
160 dims.resize (2); |
4543 | 161 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
162 dims(0) = NINTbig (tmp.double_value ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
163 dims(1) = NINTbig (tmp.double_value ()); |
2928 | 164 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
165 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
166 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
167 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
168 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
169 else if (tmp.is_range ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
170 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
171 Range r = tmp.range_value (); |
4543 | 172 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
173 if (r.all_elements_are_ints ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
174 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
175 octave_idx_type n = r.nelem (); |
4543 | 176 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
177 dims.resize (n); |
4543 | 178 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
179 octave_idx_type base = NINTbig (r.base ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
180 octave_idx_type incr = NINTbig (r.inc ()); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
181 octave_idx_type lim = NINTbig (r.limit ()); |
2928 | 182 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
183 if (base < 0 || lim < 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
184 error ("%s: all dimensions must be nonnegative", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
185 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
186 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
187 for (octave_idx_type i = 0; i < n; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
188 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
189 dims(i) = base; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
190 base += incr; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
191 } |
2928 | 192 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
193 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
194 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
195 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
196 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
197 error ("%s: expecting all elements of range to be integers", |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
198 fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
199 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
200 else if (tmp.is_matrix_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
201 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
202 Array<int> iv = tmp.int_vector_value (true); |
4543 | 203 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
204 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
205 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
206 octave_idx_type len = iv.length (); |
2928 | 207 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
208 dims.resize (len); |
4543 | 209 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
210 for (octave_idx_type i = 0; i < len; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
211 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
212 octave_idx_type elt = iv(i); |
4543 | 213 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
214 if (elt < 0) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
215 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
216 error ("%s: all dimensions must be nonnegative", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
217 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
218 } |
4543 | 219 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
220 dims(i) = iv(i); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
221 } |
2928 | 222 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
223 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
224 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
225 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
226 error ("%s: expecting integer vector", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
227 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
228 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
229 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
230 gripe_wrong_type_arg ("rand", tmp); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
231 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
232 } |
4543 | 233 } |
234 break; | |
235 | |
236 default: | |
237 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
238 octave_value tmp = args(idx); |
4543 | 239 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
240 if (nargin == 2 && tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
241 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
242 std::string ts = tmp.string_value (); |
5164 | 243 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
244 if (ts == "seed") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
245 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
246 if (args(idx+1).is_real_scalar ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
247 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
248 double d = args(idx+1).double_value (); |
2928 | 249 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
250 if (! error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
251 octave_rand::seed (d); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
252 } |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
253 else if (args(idx+1).is_string () |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
254 && args(idx+1).string_value() == "reset") |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
255 octave_rand::reset (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
256 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
257 error ("%s: seed must be a real scalar", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
258 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
259 else if (ts == "state" || ts == "twister") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
260 { |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
261 if (args(idx+1).is_string () |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
262 && args(idx+1).string_value() == "reset") |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
263 octave_rand::reset (fcn); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
264 else |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
265 { |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
266 ColumnVector s = |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
267 ColumnVector (args(idx+1).vector_value(false, true)); |
5730 | 268 |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
269 if (! error_state) |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
270 octave_rand::state (s, fcn); |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
271 } |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
272 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
273 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
274 error ("%s: unrecognized string argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
275 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
276 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
277 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
278 dims.resize (nargin); |
4543 | 279 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
280 for (int i = 0; i < nargin; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
281 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
282 dims(i) = args(idx+i).int_value (); |
4543 | 283 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
284 if (error_state) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
285 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
286 error ("%s: expecting integer arguments", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
287 goto done; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
288 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
289 } |
4543 | 290 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
291 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
292 } |
4543 | 293 } |
294 break; | |
2928 | 295 } |
296 | |
4543 | 297 done: |
2928 | 298 |
299 return retval; | |
300 | |
301 gen_matrix: | |
302 | |
5355 | 303 dims.chop_trailing_singletons (); |
304 | |
5730 | 305 if (additional_arg) |
306 { | |
307 if (a.length() == 1) | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
308 return octave_rand::nd_array (dims, a(0)); |
5730 | 309 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
310 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
311 if (a.dims() != dims) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
312 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
313 error ("%s: mismatch in argument size", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
314 return retval; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
315 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
316 octave_idx_type len = a.length (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
317 NDArray m (dims); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
318 double *v = m.fortran_vec (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
319 for (octave_idx_type i = 0; i < len; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
320 v[i] = octave_rand::scalar (a(i)); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
321 return m; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
322 } |
5730 | 323 } |
324 else | |
325 return octave_rand::nd_array (dims); | |
2928 | 326 } |
327 | |
4665 | 328 DEFUN_DLD (rand, args, , |
3369 | 329 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
330 @deftypefn {Loadable Function} {} rand (@var{x})\n\ |
3369 | 331 @deftypefnx {Loadable Function} {} rand (@var{n}, @var{m})\n\ |
5730 | 332 @deftypefnx {Loadable Function} {} rand (\"state\", @var{x})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
333 @deftypefnx {Loadable Function} {} rand (\"state\", \"reset\")\n\ |
5730 | 334 @deftypefnx {Loadable Function} {} rand (\"seed\", @var{x})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
335 @deftypefnx {Loadable Function} {} rand (\"seed\", \"reset\")\n\ |
3369 | 336 Return a matrix with random elements uniformly distributed on the\n\ |
337 interval (0, 1). The arguments are handled the same as the arguments\n\ | |
5730 | 338 for @code{eye}.\n\ |
339 \n\ | |
340 You can query the state of the random number generator using the\n\ | |
3369 | 341 form\n\ |
2928 | 342 \n\ |
3369 | 343 @example\n\ |
5730 | 344 v = rand (\"state\")\n\ |
345 @end example\n\ | |
346 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
347 This returns a column vector @var{v} of length 625. Later, you can\n\ |
5730 | 348 restore the random number generator to the state @var{v}\n\ |
349 using the form\n\ | |
350 \n\ | |
351 @example\n\ | |
352 rand (\"state\", v)\n\ | |
3369 | 353 @end example\n\ |
354 \n\ | |
355 @noindent\n\ | |
5730 | 356 You may also initialize the state vector from an arbitrary vector of\n\ |
357 length <= 625 for @var{v}. This new state will be a hash based on the\n\ | |
5798 | 358 value of @var{v}, not @var{v} itself.\n\ |
5730 | 359 \n\ |
360 By default, the generator is initialized from @code{/dev/urandom} if it is\n\ | |
361 available, otherwise from cpu time, wall clock time and the current\n\ | |
362 fraction of a second.\n\ | |
363 \n\ | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7780
diff
changeset
|
364 To compute the pseudo-random sequence, @code{rand} uses the Mersenne\n\ |
8498
5837f2a73a51
[docs] 2^19937-1 => @math{2^{19937}-1}
Brian Gough <bjg@gnu.org>
parents:
8497
diff
changeset
|
365 Twister with a period of @math{2^{19937}-1} (See M. Matsumoto and T. Nishimura,\n\ |
8482
ab51abf62698
[docs] ``Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator'' => @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator}
Brian Gough <bjg@gnu.org>
parents:
8325
diff
changeset
|
366 @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator}, ACM Trans. on\n\ |
7001 | 367 Modeling and Computer Simulation Vol. 8, No. 1, January pp.3-30 1998,\n\ |
7171 | 368 @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).\n\ |
6547 | 369 Do @strong{not} use for cryptography without securely hashing\n\ |
370 several returned values together, otherwise the generator state\n\ | |
371 can be learned after reading 624 consecutive values.\n\ | |
5730 | 372 \n\ |
7096 | 373 Older versions of Octave used a different random number generator.\n\ |
374 The new generator is used by default\n\ | |
5730 | 375 as it is significantly faster than the old generator, and produces\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
376 random numbers with a significantly longer cycle time. However, in\n\ |
5798 | 377 some circumstances it might be desirable to obtain the same random\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
378 sequences as used by the old generators. To do this the keyword\n\ |
5730 | 379 \"seed\" is used to specify that the old generators should be use,\n\ |
380 as in\n\ | |
2928 | 381 \n\ |
3369 | 382 @example\n\ |
5730 | 383 rand (\"seed\", val)\n\ |
3369 | 384 @end example\n\ |
385 \n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
386 which sets the seed of the generator to @var{val}. The seed of the\n\ |
5730 | 387 generator can be queried with\n\ |
388 \n\ | |
389 @example\n\ | |
390 s = rand (\"seed\")\n\ | |
391 @end example\n\ | |
392 \n\ | |
393 However, it should be noted that querying the seed will not cause\n\ | |
394 @code{rand} to use the old generators, only setting the seed will.\n\ | |
395 To cause @code{rand} to once again use the new generators, the\n\ | |
396 keyword \"state\" should be used to reset the state of the @code{rand}.\n\ | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
397 \n\ |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
398 The state or seed of the generator can be reset to a new random value\n\ |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
399 using the \"reset\" keyword.\n\ |
5798 | 400 @seealso{randn, rande, randg, randp}\n\ |
3369 | 401 @end deftypefn") |
2928 | 402 { |
4307 | 403 octave_value retval; |
2928 | 404 |
405 int nargin = args.length (); | |
406 | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
407 retval = do_rand (args, nargin, "rand", "uniform"); |
2928 | 408 |
409 return retval; | |
410 } | |
411 | |
8871 | 412 // FIXME -- The old generator (selected when "seed" is set) will not |
413 // work properly if compiled to use 64-bit integers. | |
414 | |
5730 | 415 /* |
416 %!test # 'state' can be a scalar | |
417 %! rand('state',12); x = rand(1,4); | |
418 %! rand('state',12); y = rand(1,4); | |
419 %! assert(x,y); | |
420 %!test # 'state' can be a vector | |
421 %! rand('state',[12,13]); x=rand(1,4); | |
422 %! rand('state',[12;13]); y=rand(1,4); | |
423 %! assert(x,y); | |
424 %!test # querying 'state' doesn't disturb sequence | |
425 %! rand('state',12); rand(1,2); x=rand(1,2); | |
426 %! rand('state',12); rand(1,2); | |
427 %! s=rand('state'); y=rand(1,2); | |
428 %! assert(x,y); | |
429 %! rand('state',s); z=rand(1,2); | |
430 %! assert(x,z); | |
431 %!test # 'seed' must be a scalar | |
432 %! rand('seed',12); x = rand(1,4); | |
433 %! rand('seed',12); y = rand(1,4); | |
434 %! assert(x,y); | |
435 %!error(rand('seed',[12,13])) | |
436 %!test # querying 'seed' returns a value which can be used later | |
437 %! s=rand('seed'); x=rand(1,2); | |
438 %! rand('seed',s); y=rand(1,2); | |
439 %! assert(x,y); | |
440 %!test # querying 'seed' doesn't disturb sequence | |
441 %! rand('seed',12); rand(1,2); x=rand(1,2); | |
442 %! rand('seed',12); rand(1,2); | |
443 %! s=rand('seed'); y=rand(1,2); | |
444 %! assert(x,y); | |
445 %! rand('seed',s); z=rand(1,2); | |
446 %! assert(x,z); | |
447 */ | |
448 | |
449 /* | |
450 %!test | |
6437 | 451 %! % Test fixed state |
452 %! rand("state",1); | |
6443 | 453 %! assert (rand(1,6), [0.1343642441124013 0.8474337369372327 0.763774618976614 0.2550690257394218 0.495435087091941 0.4494910647887382],1e-6); |
6437 | 454 %!test |
6443 | 455 %! % Test fixed seed |
6437 | 456 %! rand("seed",1); |
6443 | 457 %! assert (rand(1,6), [0.8668024251237512 0.9126510815694928 0.09366085007786751 0.1664607301354408 0.7408077004365623 0.7615650338120759],1e-6); |
5730 | 458 %!test |
6437 | 459 %! if (__random_statistical_tests__) |
460 %! % statistical tests may fail occasionally. | |
461 %! rand("state",12); | |
462 %! x = rand(100000,1); | |
463 %! assert(max(x)<1.); %*** Please report this!!! *** | |
464 %! assert(min(x)>0.); %*** Please report this!!! *** | |
465 %! assert(mean(x),0.5,0.0024); | |
466 %! assert(var(x),1/48,0.0632); | |
467 %! assert(skewness(x),0,0.012); | |
468 %! assert(kurtosis(x),-6/5,0.0094); | |
469 %! endif | |
470 %!test | |
471 %! if (__random_statistical_tests__) | |
472 %! % statistical tests may fail occasionally. | |
473 %! rand("seed",12); | |
474 %! x = rand(100000,1); | |
475 %! assert(max(x)<1.); %*** Please report this!!! *** | |
476 %! assert(min(x)>0.); %*** Please report this!!! *** | |
477 %! assert(mean(x),0.5,0.0024); | |
478 %! assert(var(x),1/48,0.0632); | |
479 %! assert(skewness(x),0,0.012); | |
480 %! assert(kurtosis(x),-6/5,0.0094); | |
481 %! endif | |
5730 | 482 */ |
483 | |
484 | |
4307 | 485 static std::string current_distribution = octave_rand::distribution (); |
486 | |
2928 | 487 static void |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
9758
diff
changeset
|
488 reset_rand_generator (void) |
2928 | 489 { |
4307 | 490 octave_rand::distribution (current_distribution); |
2928 | 491 } |
492 | |
4665 | 493 DEFUN_DLD (randn, args, , |
3369 | 494 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
495 @deftypefn {Loadable Function} {} randn (@var{x})\n\ |
3369 | 496 @deftypefnx {Loadable Function} {} randn (@var{n}, @var{m})\n\ |
5730 | 497 @deftypefnx {Loadable Function} {} randn (\"state\", @var{x})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
498 @deftypefnx {Loadable Function} {} randn (\"state\", \"reset\")\n\ |
5730 | 499 @deftypefnx {Loadable Function} {} randn (\"seed\", @var{x})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
500 @deftypefnx {Loadable Function} {} randn (\"seed\", \"reset\")\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
501 Return a matrix with normally distributed random\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
502 elements having zero mean and variance one. The arguments are\n\ |
7780
08125419efcb
Extend explanation of randn's return value
Thomas Weber <thomas.weber.mail@gmail.com>
parents:
7533
diff
changeset
|
503 handled the same as the arguments for @code{rand}.\n\ |
3369 | 504 \n\ |
8497
63d2f6508dde
[docs] Ziggurat technique => ``Ziggurat technique''
Brian Gough <bjg@gnu.org>
parents:
8483
diff
changeset
|
505 By default, @code{randn} uses the Marsaglia and Tsang ``Ziggurat technique'' to\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
506 transform from a uniform to a normal distribution. (G. Marsaglia and\n\ |
8483
b8069cab1ce3
[docs] 'Ziggurat method for generating random variables' => @cite{Ziggurat method for generating random variables}
Brian Gough <bjg@gnu.org>
parents:
8482
diff
changeset
|
507 W.W. Tsang, @cite{Ziggurat method for generating random variables},\n\ |
5730 | 508 J. Statistical Software, vol 5, 2000,\n\ |
509 @url{http://www.jstatsoft.org/v05/i08/})\n\ | |
2928 | 510 \n\ |
6547 | 511 @seealso{rand, rande, randg, randp}\n\ |
3369 | 512 @end deftypefn") |
2928 | 513 { |
4307 | 514 octave_value retval; |
2928 | 515 |
516 int nargin = args.length (); | |
517 | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
518 retval = do_rand (args, nargin, "randn", "normal"); |
2928 | 519 |
520 return retval; | |
521 } | |
522 | |
523 /* | |
5730 | 524 %!test |
6437 | 525 %! % Test fixed state |
526 %! randn("state",1); | |
6443 | 527 %! assert (randn(1,6), [-2.666521678978671 -0.7381719971724564 1.507903992673601 0.6019427189162239 -0.450661261143348 -0.7054431351574116],1e-6); |
6437 | 528 %!test |
6443 | 529 %! % Test fixed seed |
6437 | 530 %! randn("seed",1); |
6443 | 531 %! assert (randn(1,6), [-1.039402365684509 -1.25938892364502 0.1968704611063004 0.3874166905879974 -0.5976632833480835 -0.6615074276924133],1e-6); |
5730 | 532 %!test |
6437 | 533 %! if (__random_statistical_tests__) |
534 %! % statistical tests may fail occasionally. | |
535 %! randn("state",12); | |
536 %! x = randn(100000,1); | |
537 %! assert(mean(x),0,0.01); | |
538 %! assert(var(x),1,0.02); | |
539 %! assert(skewness(x),0,0.02); | |
540 %! assert(kurtosis(x),0,0.04); | |
541 %! endif | |
542 %!test | |
543 %! if (__random_statistical_tests__) | |
544 %! % statistical tests may fail occasionally. | |
545 %! randn("seed",12); | |
546 %! x = randn(100000,1); | |
547 %! assert(mean(x),0,0.01); | |
548 %! assert(var(x),1,0.02); | |
549 %! assert(skewness(x),0,0.02); | |
550 %! assert(kurtosis(x),0,0.04); | |
551 %! endif | |
5730 | 552 */ |
553 | |
554 DEFUN_DLD (rande, args, , | |
555 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
556 @deftypefn {Loadable Function} {} rande (@var{x})\n\ |
5730 | 557 @deftypefnx {Loadable Function} {} rande (@var{n}, @var{m})\n\ |
558 @deftypefnx {Loadable Function} {} rande (\"state\", @var{x})\n\ | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
559 @deftypefnx {Loadable Function} {} rande (\"state\", \"reset\")\n\ |
5730 | 560 @deftypefnx {Loadable Function} {} rande (\"seed\", @var{x})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
561 @deftypefnx {Loadable Function} {} rande (\"seed\", \"reset\")\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
562 Return a matrix with exponentially distributed random elements. The\n\ |
5730 | 563 arguments are handled the same as the arguments for @code{rand}.\n\ |
564 \n\ | |
10787
ac433932ce23
Correct typo in rande documentation (bug #30446).
Rik <octave@nomad.inbox5.com>
parents:
10782
diff
changeset
|
565 By default, @code{rande} uses the Marsaglia and Tsang ``Ziggurat technique'' to\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
566 transform from a uniform to a exponential distribution. (G. Marsaglia and\n\ |
8483
b8069cab1ce3
[docs] 'Ziggurat method for generating random variables' => @cite{Ziggurat method for generating random variables}
Brian Gough <bjg@gnu.org>
parents:
8482
diff
changeset
|
567 W.W. Tsang, @cite{Ziggurat method for generating random variables},\n\ |
5730 | 568 J. Statistical Software, vol 5, 2000,\n\ |
569 @url{http://www.jstatsoft.org/v05/i08/})\n\ | |
6547 | 570 @seealso{rand, randn, randg, randp}\n\ |
5730 | 571 @end deftypefn") |
572 { | |
573 octave_value retval; | |
574 | |
575 int nargin = args.length (); | |
576 | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
577 retval = do_rand (args, nargin, "rande", "exponential"); |
5730 | 578 |
579 return retval; | |
580 } | |
581 | |
582 /* | |
583 %!test | |
6437 | 584 %! % Test fixed state |
585 %! rande("state",1); | |
6443 | 586 %! assert (rande(1,6), [3.602973885835625 0.1386190677555021 0.6743112889616958 0.4512830847258422 0.7255744741233175 0.3415969205292291],1e-6); |
6437 | 587 %!test |
6443 | 588 %! % Test fixed seed |
6437 | 589 %! rande("seed",1); |
6443 | 590 %! assert (rande(1,6), [0.06492075175653866 1.717980206012726 0.4816154008731246 0.5231300676241517 0.103910739364359 1.668931916356087],1e-6); |
5730 | 591 %!test |
6437 | 592 %! if (__random_statistical_tests__) |
593 %! % statistical tests may fail occasionally | |
594 %! rande("state",1); | |
595 %! x = rande(100000,1); | |
596 %! assert(min(x)>0); % *** Please report this!!! *** | |
597 %! assert(mean(x),1,0.01); | |
598 %! assert(var(x),1,0.03); | |
599 %! assert(skewness(x),2,0.06); | |
600 %! assert(kurtosis(x),6,0.7); | |
601 %! endif | |
602 %!test | |
603 %! if (__random_statistical_tests__) | |
604 %! % statistical tests may fail occasionally | |
605 %! rande("seed",1); | |
606 %! x = rande(100000,1); | |
607 %! assert(min(x)>0); % *** Please report this!!! *** | |
608 %! assert(mean(x),1,0.01); | |
609 %! assert(var(x),1,0.03); | |
610 %! assert(skewness(x),2,0.06); | |
611 %! assert(kurtosis(x),6,0.7); | |
612 %! endif | |
5730 | 613 */ |
614 | |
615 DEFUN_DLD (randg, args, , | |
616 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
617 @deftypefn {Loadable Function} {} randg (@var{a}, @var{x})\n\ |
5730 | 618 @deftypefnx {Loadable Function} {} randg (@var{a}, @var{n}, @var{m})\n\ |
619 @deftypefnx {Loadable Function} {} randg (\"state\", @var{x})\n\ | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
620 @deftypefnx {Loadable Function} {} randg (\"state\", \"reset\")\n\ |
5730 | 621 @deftypefnx {Loadable Function} {} randg (\"seed\", @var{x})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
622 @deftypefnx {Loadable Function} {} randg (\"seed\", \"reset\")\n\ |
5730 | 623 Return a matrix with @code{gamma(@var{a},1)} distributed random elements.\n\ |
624 The arguments are handled the same as the arguments for @code{rand},\n\ | |
625 except for the argument @var{a}.\n\ | |
626 \n\ | |
627 This can be used to generate many distributions:\n\ | |
628 \n\ | |
629 @table @asis\n\ | |
6547 | 630 @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0}\n\ |
5730 | 631 @example\n\ |
6547 | 632 r = b * randg (a)\n\ |
5730 | 633 @end example\n\ |
6547 | 634 @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1}\n\ |
5730 | 635 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
636 @group\n\ |
6547 | 637 r1 = randg (a, 1)\n\ |
638 r = r1 / (r1 + randg (b, 1))\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
639 @end group\n\ |
5730 | 640 @end example\n\ |
6547 | 641 @item @code{Erlang (a, n)}\n\ |
5730 | 642 @example\n\ |
6547 | 643 r = a * randg (n)\n\ |
5730 | 644 @end example\n\ |
6547 | 645 @item @code{chisq (df)} for @code{df > 0}\n\ |
5730 | 646 @example\n\ |
6547 | 647 r = 2 * randg (df / 2)\n\ |
5730 | 648 @end example\n\ |
649 @item @code{t(df)} for @code{0 < df < inf} (use randn if df is infinite)\n\ | |
650 @example\n\ | |
6547 | 651 r = randn () / sqrt (2 * randg (df / 2) / df)\n\ |
5730 | 652 @end example\n\ |
6547 | 653 @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2}\n\ |
5730 | 654 @example\n\ |
7096 | 655 @group\n\ |
656 ## r1 equals 1 if n1 is infinite\n\ | |
657 r1 = 2 * randg (n1 / 2) / n1\n\ | |
658 ## r2 equals 1 if n2 is infinite\n\ | |
659 r2 = 2 * randg (n2 / 2) / n2\n\ | |
5730 | 660 r = r1 / r2\n\n\ |
7096 | 661 @end group\n\ |
5730 | 662 @end example\n\ |
663 @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1}\n\ | |
664 @example\n\ | |
6547 | 665 r = randp ((1 - p) / p * randg (n))\n\ |
5730 | 666 @end example\n\ |
6547 | 667 @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0}\n\ |
5730 | 668 (use chisq if @code{L = 0})\n\ |
669 @example\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
670 @group\n\ |
6547 | 671 r = randp (L / 2)\n\ |
672 r(r > 0) = 2 * randg (r(r > 0))\n\ | |
673 r(df > 0) += 2 * randg (df(df > 0)/2)\n\ | |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
674 @end group\n\ |
5730 | 675 @end example\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
676 @item @code{Dirichlet (a1, @dots{} ak)}\n\ |
5730 | 677 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
678 @group\n\ |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
679 r = (randg (a1), @dots{}, randg (ak))\n\ |
6547 | 680 r = r / sum (r)\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9041
diff
changeset
|
681 @end group\n\ |
5730 | 682 @end example\n\ |
683 @end table\n\ | |
6547 | 684 @seealso{rand, randn, rande, randp}\n\ |
5730 | 685 @end deftypefn") |
686 { | |
687 octave_value retval; | |
688 | |
689 int nargin = args.length (); | |
690 | |
691 if (nargin < 1) | |
692 error ("randg: insufficient arguments"); | |
693 else | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
694 retval = do_rand (args, nargin, "randg", "gamma", true); |
5730 | 695 |
696 return retval; | |
697 } | |
698 | |
699 /* | |
700 %!test | |
6437 | 701 %! randg("state",12) |
702 %!assert(randg([-inf,-1,0,inf,nan]),[nan,nan,nan,nan,nan]) % *** Please report | |
703 | |
704 | |
705 %!test | |
706 %! % Test fixed state | |
707 %! randg("state",1); | |
6443 | 708 %! assert (randg(0.1,1,6), [0.0103951513331241 8.335671459898252e-05 0.00138691397249762 0.000587308416993855 0.495590518784736 2.3921917414795e-12],1e-6); |
6437 | 709 %!test |
710 %! % Test fixed state | |
711 %! randg("state",1); | |
6443 | 712 %! assert (randg(0.95,1,6), [3.099382433255327 0.3974529788871218 0.644367450750855 1.143261091802246 1.964111762696822 0.04011915547957939],1e-6); |
6437 | 713 %!test |
714 %! % Test fixed state | |
715 %! randg("state",1); | |
6443 | 716 %! assert (randg(1,1,6), [0.2273389379645993 1.288822625058359 0.2406335209340746 1.218869553370733 1.024649860162554 0.09631230343599533],1e-6); |
6437 | 717 %!test |
718 %! % Test fixed state | |
719 %! randg("state",1); | |
6443 | 720 %! assert (randg(10,1,6), [3.520369644331133 15.15369864472106 8.332112081991205 8.406211067432674 11.81193475187611 10.88792728177059],1e-5); |
6437 | 721 %!test |
722 %! % Test fixed state | |
723 %! randg("state",1); | |
6443 | 724 %! assert (randg(100,1,6), [75.34570255262264 115.4911985594699 95.23493031356388 95.48926019250911 106.2397448229803 103.4813150404118],1e-4); |
6437 | 725 %!test |
726 %! % Test fixed seed | |
727 %! randg("seed",1); | |
6443 | 728 %! assert (randg(0.1,1,6), [0.07144210487604141 0.460641473531723 0.4749028384685516 0.06823389977216721 0.000293838675133884 1.802567535340305e-12],1e-6); |
6437 | 729 %!test |
730 %! % Test fixed seed | |
731 %! randg("seed",1); | |
6443 | 732 %! assert (randg(0.95,1,6), [1.664905071258545 1.879976987838745 1.905677795410156 0.9948706030845642 0.5606933236122131 0.0766092911362648],1e-6); |
6437 | 733 %!test |
734 %! % Test fixed seed | |
735 %! randg("seed",1); | |
6443 | 736 %! assert (randg(1,1,6), [0.03512085229158401 0.6488978862762451 0.8114678859710693 0.1666885763406754 1.60791552066803 1.90356981754303],1e-6); |
6437 | 737 %!test |
738 %! % Test fixed seed | |
739 %! randg("seed",1); | |
6443 | 740 %! assert (randg(10,1,6), [6.566435813903809 10.11648464202881 10.73162078857422 7.747178077697754 6.278522491455078 6.240195751190186],1e-5); |
6437 | 741 %!test |
742 %! % Test fixed seed | |
743 %! randg("seed",1); | |
6443 | 744 %! assert (randg(100,1,6), [89.40208435058594 101.4734725952148 103.4020004272461 93.62763214111328 88.33104705810547 88.1871337890625],1e-4); |
6437 | 745 %!test |
746 %! if (__random_statistical_tests__) | |
747 %! % statistical tests may fail occasionally. | |
748 %! randg("state",12) | |
749 %! a=0.1; x = randg(a,100000,1); | |
750 %! assert(mean(x), a, 0.01); | |
751 %! assert(var(x), a, 0.01); | |
752 %! assert(skewness(x),2/sqrt(a), 1.); | |
753 %! assert(kurtosis(x),6/a, 50.); | |
754 %! endif | |
755 %!test | |
756 %! if (__random_statistical_tests__) | |
757 %! % statistical tests may fail occasionally. | |
758 %! randg("state",12) | |
759 %! a=0.95; x = randg(a,100000,1); | |
760 %! assert(mean(x), a, 0.01); | |
761 %! assert(var(x), a, 0.04); | |
762 %! assert(skewness(x),2/sqrt(a), 0.2); | |
763 %! assert(kurtosis(x),6/a, 2.); | |
764 %! endif | |
765 %!test | |
766 %! if (__random_statistical_tests__) | |
767 %! % statistical tests may fail occasionally. | |
768 %! randg("state",12) | |
769 %! a=1; x = randg(a,100000,1); | |
770 %! assert(mean(x),a, 0.01); | |
771 %! assert(var(x),a, 0.04); | |
772 %! assert(skewness(x),2/sqrt(a), 0.2); | |
773 %! assert(kurtosis(x),6/a, 2.); | |
774 %! endif | |
775 %!test | |
776 %! if (__random_statistical_tests__) | |
777 %! % statistical tests may fail occasionally. | |
778 %! randg("state",12) | |
779 %! a=10; x = randg(a,100000,1); | |
780 %! assert(mean(x), a, 0.1); | |
781 %! assert(var(x), a, 0.5); | |
782 %! assert(skewness(x),2/sqrt(a), 0.1); | |
783 %! assert(kurtosis(x),6/a, 0.5); | |
784 %! endif | |
785 %!test | |
786 %! if (__random_statistical_tests__) | |
787 %! % statistical tests may fail occasionally. | |
788 %! randg("state",12) | |
789 %! a=100; x = randg(a,100000,1); | |
790 %! assert(mean(x), a, 0.2); | |
791 %! assert(var(x), a, 2.); | |
792 %! assert(skewness(x),2/sqrt(a), 0.05); | |
793 %! assert(kurtosis(x),6/a, 0.2); | |
794 %! endif | |
795 %!test | |
796 %! randg("seed",12) | |
5730 | 797 %!assert(randg([-inf,-1,0,inf,nan]),[nan,nan,nan,nan,nan]) % *** Please report |
798 %!test | |
6437 | 799 %! if (__random_statistical_tests__) |
800 %! % statistical tests may fail occasionally. | |
801 %! randg("seed",12) | |
802 %! a=0.1; x = randg(a,100000,1); | |
803 %! assert(mean(x), a, 0.01); | |
804 %! assert(var(x), a, 0.01); | |
805 %! assert(skewness(x),2/sqrt(a), 1.); | |
806 %! assert(kurtosis(x),6/a, 50.); | |
807 %! endif | |
5730 | 808 %!test |
6437 | 809 %! if (__random_statistical_tests__) |
810 %! % statistical tests may fail occasionally. | |
811 %! randg("seed",12) | |
812 %! a=0.95; x = randg(a,100000,1); | |
813 %! assert(mean(x), a, 0.01); | |
814 %! assert(var(x), a, 0.04); | |
815 %! assert(skewness(x),2/sqrt(a), 0.2); | |
816 %! assert(kurtosis(x),6/a, 2.); | |
817 %! endif | |
5730 | 818 %!test |
6437 | 819 %! if (__random_statistical_tests__) |
820 %! % statistical tests may fail occasionally. | |
821 %! randg("seed",12) | |
822 %! a=1; x = randg(a,100000,1); | |
823 %! assert(mean(x),a, 0.01); | |
824 %! assert(var(x),a, 0.04); | |
825 %! assert(skewness(x),2/sqrt(a), 0.2); | |
826 %! assert(kurtosis(x),6/a, 2.); | |
827 %! endif | |
5730 | 828 %!test |
6437 | 829 %! if (__random_statistical_tests__) |
830 %! % statistical tests may fail occasionally. | |
831 %! randg("seed",12) | |
832 %! a=10; x = randg(a,100000,1); | |
833 %! assert(mean(x), a, 0.1); | |
834 %! assert(var(x), a, 0.5); | |
835 %! assert(skewness(x),2/sqrt(a), 0.1); | |
836 %! assert(kurtosis(x),6/a, 0.5); | |
837 %! endif | |
5730 | 838 %!test |
6437 | 839 %! if (__random_statistical_tests__) |
840 %! % statistical tests may fail occasionally. | |
841 %! randg("seed",12) | |
842 %! a=100; x = randg(a,100000,1); | |
843 %! assert(mean(x), a, 0.2); | |
844 %! assert(var(x), a, 2.); | |
845 %! assert(skewness(x),2/sqrt(a), 0.05); | |
846 %! assert(kurtosis(x),6/a, 0.2); | |
847 %! endif | |
5730 | 848 */ |
849 | |
850 | |
851 DEFUN_DLD (randp, args, , | |
852 "-*- texinfo -*-\n\ | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
853 @deftypefn {Loadable Function} {} randp (@var{l}, @var{x})\n\ |
5730 | 854 @deftypefnx {Loadable Function} {} randp (@var{l}, @var{n}, @var{m})\n\ |
855 @deftypefnx {Loadable Function} {} randp (\"state\", @var{x})\n\ | |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
856 @deftypefnx {Loadable Function} {} randp (\"state\", \"reset\")\n\ |
5730 | 857 @deftypefnx {Loadable Function} {} randp (\"seed\", @var{x})\n\ |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
858 @deftypefnx {Loadable Function} {} randp (\"seed\", \"reset\")\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
859 Return a matrix with Poisson distributed random elements with mean value\n\ |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
860 parameter given by the first argument, @var{l}. The arguments\n\ |
5730 | 861 are handled the same as the arguments for @code{rand}, except for the\n\ |
862 argument @var{l}.\n\ | |
863 \n\ | |
864 Five different algorithms are used depending on the range of @var{l}\n\ | |
865 and whether or not @var{l} is a scalar or a matrix.\n\ | |
866 \n\ | |
867 @table @asis\n\ | |
868 @item For scalar @var{l} <= 12, use direct method.\n\ | |
869 Press, et al., 'Numerical Recipes in C', Cambridge University Press, 1992.\n\ | |
870 @item For scalar @var{l} > 12, use rejection method.[1]\n\ | |
871 Press, et al., 'Numerical Recipes in C', Cambridge University Press, 1992.\n\ | |
872 @item For matrix @var{l} <= 10, use inversion method.[2]\n\ | |
873 Stadlober E., et al., WinRand source code, available via FTP.\n\ | |
874 @item For matrix @var{l} > 10, use patchwork rejection method.\n\ | |
875 Stadlober E., et al., WinRand source code, available via FTP, or\n\ | |
876 H. Zechner, 'Efficient sampling from continuous and discrete\n\ | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7780
diff
changeset
|
877 unimodal distributions', Doctoral Dissertation, 156pp., Technical\n\ |
5730 | 878 University Graz, Austria, 1994.\n\ |
879 @item For @var{l} > 1e8, use normal approximation.\n\ | |
880 L. Montanet, et al., 'Review of Particle Properties', Physical Review\n\ | |
881 D 50 p1284, 1994\n\ | |
882 @end table\n\ | |
6547 | 883 @seealso{rand, randn, rande, randg}\n\ |
5730 | 884 @end deftypefn") |
885 { | |
886 octave_value retval; | |
887 | |
888 int nargin = args.length (); | |
889 | |
890 if (nargin < 1) | |
891 error ("randp: insufficient arguments"); | |
892 else | |
10782
d1f920d1ce0c
simplify code in rand.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10709
diff
changeset
|
893 retval = do_rand (args, nargin, "randp", "poisson", true); |
5730 | 894 |
895 return retval; | |
896 } | |
897 | |
898 /* | |
899 %!test | |
6437 | 900 %! randp("state",12) |
901 %!assert(randp([-inf,-1,0,inf,nan]),[nan,nan,0,nan,nan]); % *** Please report | |
902 %!test | |
903 %! % Test fixed state | |
904 %! randp("state",1); | |
905 %! assert(randp(5,1,6),[5 5 3 7 7 3]) | |
906 %!test | |
907 %! % Test fixed state | |
908 %! randp("state",1); | |
909 %! assert(randp(15,1,6),[13 15 8 18 18 15]) | |
910 %!test | |
911 %! % Test fixed state | |
912 %! randp("state",1); | |
7421 | 913 %! assert(randp(1e9,1,6),[999915677 999976657 1000047684 1000019035 999985749 999977692],-1e-6) |
6437 | 914 %!test |
915 %! % Test fixed state | |
916 %! randp("seed",1); | |
6449 | 917 %! %%assert(randp(5,1,6),[8 2 3 6 6 8]) |
918 %! assert(randp(5,1,5),[8 2 3 6 6]) | |
6437 | 919 %!test |
920 %! % Test fixed state | |
921 %! randp("seed",1); | |
922 %! assert(randp(15,1,6),[15 16 12 10 10 12]) | |
923 %!test | |
924 %! % Test fixed state | |
925 %! randp("seed",1); | |
7421 | 926 %! assert(randp(1e9,1,6),[1000006208 1000012224 999981120 999963520 999963072 999981440],-1e-6) |
6437 | 927 %!test |
928 %! if (__random_statistical_tests__) | |
929 %! % statistical tests may fail occasionally. | |
930 %! randp("state",12) | |
931 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
932 %! x = randp(a(1),100000,1); | |
933 %! assert(min(x)>=0); % *** Please report this!!! *** | |
934 %! assert(mean(x),a(1),a(2)); | |
935 %! assert(var(x),a(1),0.02*a(1)); | |
936 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
937 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
938 %! endfor | |
939 %! endif | |
940 %!test | |
941 %! if (__random_statistical_tests__) | |
942 %! % statistical tests may fail occasionally. | |
943 %! randp("state",12) | |
944 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
945 %! x = randp(a(1)*ones(100000,1),100000,1); | |
946 %! assert(min(x)>=0); % *** Please report this!!! *** | |
947 %! assert(mean(x),a(1),a(2)); | |
948 %! assert(var(x),a(1),0.02*a(1)); | |
949 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
950 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
951 %! endfor | |
952 %! endif | |
953 %!test | |
954 %! randp("seed",12) | |
5730 | 955 %!assert(randp([-inf,-1,0,inf,nan]),[nan,nan,0,nan,nan]); % *** Please report |
956 %!test | |
6449 | 957 %! if (__random_statistical_tests__) |
958 %! % statistical tests may fail occasionally. | |
959 %! randp("seed",12) | |
960 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
961 %! x = randp(a(1),100000,1); | |
962 %! assert(min(x)>=0); % *** Please report this!!! *** | |
963 %! assert(mean(x),a(1),a(2)); | |
964 %! assert(var(x),a(1),0.02*a(1)); | |
965 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
966 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
967 %! endfor | |
968 %! endif | |
5730 | 969 %!test |
6449 | 970 %! if (__random_statistical_tests__) |
971 %! % statistical tests may fail occasionally. | |
972 %! randp("seed",12) | |
973 %! for a=[5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] | |
974 %! x = randp(a(1)*ones(100000,1),100000,1); | |
975 %! assert(min(x)>=0); % *** Please report this!!! *** | |
976 %! assert(mean(x),a(1),a(2)); | |
977 %! assert(var(x),a(1),0.02*a(1)); | |
978 %! assert(skewness(x),1/sqrt(a(1)),a(3)); | |
979 %! assert(kurtosis(x),1/a(1),3*a(3)); | |
980 %! endfor | |
981 %! endif | |
5730 | 982 */ |
983 | |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
984 DEFUN_DLD (randperm, args, , |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
985 "-*- texinfo -*-\n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10155
diff
changeset
|
986 @deftypefn {Loadable Function} {} randperm (@var{n})\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
987 @deftypefnx {Loadable Function} {} randperm (@var{n}, @var{m})\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
988 Return a row vector containing a random permutation of @code{1:@var{n}}.\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
989 If @var{m} is supplied, return @var{m} permutations,\n\ |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9647
diff
changeset
|
990 one in each row of a NxM matrix. The complexity is O(M*N) in both time and\n\ |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9647
diff
changeset
|
991 memory. The randomization is performed using rand().\n\ |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
992 All permutations are equally likely.\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
993 @seealso{perms}\n\ |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
994 @end deftypefn") |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
995 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
996 int nargin = args.length (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
997 octave_value retval; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
998 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
999 if (nargin == 1 || nargin == 2) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1000 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1001 octave_idx_type n, m; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1002 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1003 if (nargin == 2) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1004 m = args(1).idx_type_value (true); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1005 else |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1006 m = 1; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1007 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1008 n = args(0).idx_type_value (true); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1009 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1010 if (m < 0 || n < 0) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1011 error ("randperm: m and n must be non-negative"); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1012 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1013 if (! error_state) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1014 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1015 // Generate random numbers. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1016 NDArray r = octave_rand::nd_array (dim_vector (m, n)); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1017 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1018 // Create transposed to allow faster access. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1019 Array<octave_idx_type> idx (dim_vector (n, m)); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1020 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1021 double *rvec = r.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1022 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1023 octave_idx_type *ivec = idx.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1024 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1025 // Perform the Knuth shuffle. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1026 for (octave_idx_type j = 0; j < m; j++) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1027 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1028 for (octave_idx_type i = 0; i < n; i++) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1029 ivec[i] = i; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1030 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1031 for (octave_idx_type i = 0; i < n; i++) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1032 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1033 octave_idx_type k = i + floor (rvec[i] * (n - i)); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1034 std::swap (ivec[i], ivec[k]); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1035 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1036 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1037 ivec += n; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1038 rvec += n; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1039 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1040 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1041 // Transpose. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1042 idx = idx.transpose (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1043 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1044 // Re-fetch the pointers. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1045 ivec = idx.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1046 rvec = r.fortran_vec (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1047 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1048 // Convert to doubles, reusing r. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1049 for (octave_idx_type i = 0, l = m*n; i < l; i++) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1050 rvec[i] = ivec[i] + 1; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1051 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1052 // Now create an array object with a cached idx_vector. |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1053 retval = new octave_matrix (r, idx_vector (idx)); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1054 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1055 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1056 else |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1057 print_usage (); |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1058 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1059 return retval; |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1060 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1061 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1062 /* |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1063 %!assert(sort(randperm(20)),1:20) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1064 %!assert(sort(randperm(20,50),2),repmat(1:20,50,1)) |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1065 */ |