Mercurial > octave
annotate libinterp/corefcn/rand.cc @ 28823:26cfccfee9a0
Replace unwind_protect with more efficient constructs (bug #59192).
* debug.cc, error.cc, ft-text-renderer.cc, load-path.cc, mex.cc, oct-hist.cc,
pager.cc, rand.cc, strfns.cc, sysdep.cc, toplev.cc, variables.cc,
__delaunayn__.cc, __voronoi__.cc, audiodevinfo.cc, audioread.cc, convhulln.cc,
ov-class.cc, ov-struct.cc: Remove unwind_protect frames declared but never used.
Replace frame.protect_var () instances with unwind_protect_var<> object.
Replace frame.add_fcn () instances with unwind_action<> object.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 30 Sep 2020 03:17:52 -0700 |
parents | 98192ec1621f |
children | 445e5ac1f58d |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 /////////////////////////////////////////////////////////////////////////* |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
3 // Copyright (C) 1996-2020 The Octave Project Developers |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
2928 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21580
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21234
diff
changeset
|
27 # include "config.h" |
2928 | 28 #endif |
29 | |
30 #include <ctime> | |
22150
c4901d7a2d7d
Remove check for std::tr1::unordered_map because we depend on C++11.
Carnë Draug <carandraug@octave.org>
parents:
21968
diff
changeset
|
31 #include <unordered_map> |
2928 | 32 #include <string> |
33 | |
34 #include "f77-fcn.h" | |
35 #include "lo-mappers.h" | |
4307 | 36 #include "oct-rand.h" |
4153 | 37 #include "quit.h" |
2928 | 38 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
39 #include "defun.h" |
2928 | 40 #include "error.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
21041
diff
changeset
|
41 #include "errwarn.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20894
diff
changeset
|
42 #include "ovl.h" |
2928 | 43 #include "unwind-prot.h" |
44 #include "utils.h" | |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
45 #include "ov-re-mat.h" |
2928 | 46 |
6437 | 47 /* |
24816
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
48 %% Restore all rand* "seed" and "state" values in order, so that the |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
49 %% new "state" algorithm remains active after these tests complete. |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
50 %!function restore_rand_states (seed, state) |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
51 %! rand ("seed", seed.rand); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
52 %! rande ("seed", seed.rande); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
53 %! randg ("seed", seed.randg); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
54 %! randn ("seed", seed.randn); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
55 %! randp ("seed", seed.randp); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
56 %! rand ("state", state.rand); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
57 %! rande ("state", state.rande); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
58 %! randg ("state", state.randg); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
59 %! randn ("state", state.randn); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
60 %! randp ("state", state.randp); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
61 %!endfunction |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
62 |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
63 %!shared __random_statistical_tests__, old_seed, old_state, restore_state |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
64 %! ## Flag whether the statistical tests should be run in "make check" or not |
6437 | 65 %! __random_statistical_tests__ = 0; |
24816
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
66 %! ## Save and restore the states of each of the random number generators |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
67 %! ## that are tested by the unit tests in this file. |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
68 %! old_seed.rand = rand ("seed"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
69 %! old_seed.rande = rande ("seed"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
70 %! old_seed.randg = randg ("seed"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
71 %! old_seed.randn = randn ("seed"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
72 %! old_seed.randp = randp ("seed"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
73 %! old_state.rand = rand ("state"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
74 %! old_state.rande = rande ("state"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
75 %! old_state.randg = randg ("state"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
76 %! old_state.randn = randn ("state"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
77 %! old_state.randp = randp ("state"); |
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
78 %! restore_state = onCleanup (@() restore_rand_states (old_seed, old_state)); |
6437 | 79 */ |
80 | |
4307 | 81 static octave_value |
5730 | 82 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
|
83 const std::string& distribution, bool additional_arg = false) |
2928 | 84 { |
5730 | 85 NDArray a; |
86 int idx = 0; | |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
87 bool is_single = false; |
2928 | 88 |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14706
diff
changeset
|
89 if (nargin > 0 && args(nargin-1).is_string ()) |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
90 { |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
91 std::string s_arg = args(nargin-1).string_value (); |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
92 |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
93 if (s_arg == "single") |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
94 { |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
95 is_single = true; |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
96 nargin--; |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
97 } |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
98 else if (s_arg == "double") |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
99 nargin--; |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
100 } |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
101 |
5730 | 102 if (additional_arg) |
103 { | |
104 if (nargin == 0) | |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
105 error ("%s: at least one argument is required", fcn); |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14706
diff
changeset
|
106 else if (args(0).is_string ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
107 additional_arg = false; |
5730 | 108 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
109 { |
20711
7b608fadc663
Make error messages more specific about the variable and problem encountered.
Rik <rik@octave.org>
parents:
20704
diff
changeset
|
110 a = args(0).xarray_value ("%s: dimension must be a scalar integer", fcn); |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20687
diff
changeset
|
111 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
112 idx++; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
113 nargin--; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
114 } |
5730 | 115 } |
2928 | 116 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
117 octave_value retval; |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
118 dim_vector dims; |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
119 |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
120 // Restore current distribution on any exit. |
28823
26cfccfee9a0
Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents:
28638
diff
changeset
|
121 octave::unwind_action restore_distribution |
26cfccfee9a0
Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents:
28638
diff
changeset
|
122 ([] (const auto& old_distribution) |
26cfccfee9a0
Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents:
28638
diff
changeset
|
123 { |
26cfccfee9a0
Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents:
28638
diff
changeset
|
124 octave::rand::distribution (old_distribution); |
26cfccfee9a0
Replace unwind_protect with more efficient constructs (bug #59192).
Rik <rik@octave.org>
parents:
28638
diff
changeset
|
125 }, octave::rand::distribution ()); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
126 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
127 octave::rand::distribution (distribution); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
128 |
4543 | 129 switch (nargin) |
2928 | 130 { |
4543 | 131 case 0: |
132 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
133 if (additional_arg) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
134 dims = a.dims (); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
135 else |
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 dims.resize (2); |
4543 | 138 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
139 dims(0) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
140 dims(1) = 1; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
141 } |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
142 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
143 goto gen_matrix; |
4543 | 144 } |
145 break; | |
2928 | 146 |
4543 | 147 case 1: |
148 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
149 octave_value tmp = args(idx); |
4543 | 150 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
151 if (tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
152 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
153 std::string s_arg = tmp.string_value (); |
2928 | 154 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
155 if (s_arg == "dist") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
156 retval = octave::rand::distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
157 else if (s_arg == "seed") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
158 retval = octave::rand::seed (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
159 else if (s_arg == "state" || s_arg == "twister") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
160 retval = octave::rand::state (fcn); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
161 else if (s_arg == "uniform") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
162 octave::rand::uniform_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
163 else if (s_arg == "normal") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
164 octave::rand::normal_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
165 else if (s_arg == "exponential") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
166 octave::rand::exponential_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
167 else if (s_arg == "poisson") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
168 octave::rand::poisson_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
169 else if (s_arg == "gamma") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
170 octave::rand::gamma_distribution (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
171 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
172 error ("%s: unrecognized string argument", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
173 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
174 else if (tmp.is_scalar_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
175 { |
27249
84cdeeaf0dce
Accept only integers for single input form of rand (bug #56609).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
176 octave_idx_type n = tmp.idx_type_value (true); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
177 |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
178 dims.resize (2); |
4543 | 179 |
27249
84cdeeaf0dce
Accept only integers for single input form of rand (bug #56609).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
180 dims(0) = dims(1) = n; |
2928 | 181 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
182 goto gen_matrix; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
183 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
184 else if (tmp.is_range ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
185 { |
28638
98192ec1621f
replace Range with range<double>
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
186 octave::range<double> r = tmp.range_value (); |
4543 | 187 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
188 if (! r.all_elements_are_ints ()) |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
189 error ("%s: all elements of range must be integers", fcn); |
4543 | 190 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
191 octave_idx_type n = r.numel (); |
4543 | 192 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
193 dims.resize (n); |
2928 | 194 |
21782
2aef506f3fec
use namespace for lo-mappers.h functions
John W. Eaton <jwe@octave.org>
parents:
21743
diff
changeset
|
195 octave_idx_type base = octave::math::nint_big (r.base ()); |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28598
diff
changeset
|
196 octave_idx_type incr = octave::math::nint_big (r.increment ()); |
2928 | 197 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
198 for (octave_idx_type i = 0; i < n; i++) |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
199 { |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
200 // Negative dimensions treated as zero for Matlab compatibility |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
201 dims(i) = (base >= 0 ? base : 0); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
202 base += incr; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
203 } |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
204 |
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
205 goto gen_matrix; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
206 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
207 else if (tmp.is_matrix_type ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
208 { |
25927
d6581134daaa
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension in a dimension vector (bug #54781).
Rik <rik@octave.org>
parents:
25926
diff
changeset
|
209 Array<octave_idx_type> iv; |
4543 | 210 |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
211 try |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
212 { |
25927
d6581134daaa
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension in a dimension vector (bug #54781).
Rik <rik@octave.org>
parents:
25926
diff
changeset
|
213 iv = tmp.octave_idx_type_vector_value (true); |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
214 } |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
215 catch (octave::execution_exception& e) |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
216 { |
20756
200ae1d650b7
propagate octave_execution_exception objects through try/catch blocks
John W. Eaton <jwe@octave.org>
parents:
20743
diff
changeset
|
217 error (e, "%s: dimensions must be a scalar or array of integers", fcn); |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
218 } |
4543 | 219 |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
220 octave_idx_type len = iv.numel (); |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
221 |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
222 dims.resize (len); |
2928 | 223 |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
224 for (octave_idx_type i = 0; i < len; i++) |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
225 { |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
226 // Negative dimensions treated as zero for Matlab compatibility |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
227 octave_idx_type elt = iv(i); |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
228 dims(i) = (elt >=0 ? elt : 0); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
229 } |
20743
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
230 |
b6408331bfa2
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20711
diff
changeset
|
231 goto gen_matrix; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
232 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
233 else |
21129
228b65504557
maint: Eliminate useless statements after err_XXX.
Rik <rik@octave.org>
parents:
21127
diff
changeset
|
234 err_wrong_type_arg ("rand", tmp); |
4543 | 235 } |
236 break; | |
237 | |
238 default: | |
239 { | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
240 octave_value tmp = args(idx); |
4543 | 241 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
242 if (nargin == 2 && tmp.is_string ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
243 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
244 std::string ts = tmp.string_value (); |
5164 | 245 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
246 if (ts == "seed") |
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 if (args(idx+1).is_real_scalar ()) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
249 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
250 double d = args(idx+1).double_value (); |
2928 | 251 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
252 octave::rand::seed (d); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
253 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
254 else if (args(idx+1).is_string () |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14706
diff
changeset
|
255 && args(idx+1).string_value () == "reset") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
256 octave::rand::reset (); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
257 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
258 error ("%s: seed must be a real scalar", fcn); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
259 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
260 else if (ts == "state" || ts == "twister") |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
261 { |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
262 if (args(idx+1).is_string () |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14706
diff
changeset
|
263 && args(idx+1).string_value () == "reset") |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
264 octave::rand::reset (fcn); |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
265 else |
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
266 { |
27277
db687716fed6
style fixes: generally aim to break long lines before operators, not after
John W. Eaton <jwe@octave.org>
parents:
27249
diff
changeset
|
267 ColumnVector s |
db687716fed6
style fixes: generally aim to break long lines before operators, not after
John W. Eaton <jwe@octave.org>
parents:
27249
diff
changeset
|
268 = ColumnVector (args(idx+1).vector_value (false, true)); |
25946
85f499b88c43
maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents:
25651
diff
changeset
|
269 |
25651
c759aa39c23a
Produce same random number stream for "state" initialized to Inf (bug #54342).
Rik <rik@octave.org>
parents:
25143
diff
changeset
|
270 // Backwards compatibility with previous versions of |
c759aa39c23a
Produce same random number stream for "state" initialized to Inf (bug #54342).
Rik <rik@octave.org>
parents:
25143
diff
changeset
|
271 // Octave which mapped Inf to 0. |
c759aa39c23a
Produce same random number stream for "state" initialized to Inf (bug #54342).
Rik <rik@octave.org>
parents:
25143
diff
changeset
|
272 for (octave_idx_type i = 0; i < s.numel (); i++) |
c759aa39c23a
Produce same random number stream for "state" initialized to Inf (bug #54342).
Rik <rik@octave.org>
parents:
25143
diff
changeset
|
273 if (octave::math::isinf (s.xelem (i))) |
c759aa39c23a
Produce same random number stream for "state" initialized to Inf (bug #54342).
Rik <rik@octave.org>
parents:
25143
diff
changeset
|
274 s.xelem (i) = 0.0; |
5730 | 275 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
276 octave::rand::state (s, fcn); |
10709
92a85ed5b86e
Don't special case color_property type when emitting factory default (bug #30118)
David Bateman <dbateman@free.fr>
parents:
10687
diff
changeset
|
277 } |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
278 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
279 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
280 error ("%s: unrecognized string argument", fcn); |
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 else |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
283 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
284 dims.resize (nargin); |
4543 | 285 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
286 for (int i = 0; i < nargin; i++) |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
287 { |
25926
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
288 octave_idx_type elt = args(idx+i).idx_type_value (true); |
20704
571508c1ed06
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20687
diff
changeset
|
289 |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
290 // Negative dimensions treated as zero for Matlab compatibility |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
291 dims(i) = (elt >= 0 ? elt : 0); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
292 } |
4543 | 293 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
294 goto gen_matrix; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
295 } |
4543 | 296 } |
297 break; | |
2928 | 298 } |
299 | |
21170
1edaf1ee65eb
maint: Replace some uses of goto in libinterp.
Rik <rik@octave.org>
parents:
21129
diff
changeset
|
300 // No "goto gen_matrix" in code path. Must be done processing. |
2928 | 301 return retval; |
302 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
303 gen_matrix: |
2928 | 304 |
5355 | 305 dims.chop_trailing_singletons (); |
306 | |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
307 if (is_single) |
5730 | 308 { |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
309 if (additional_arg) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
310 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
311 if (a.numel () == 1) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
312 return octave::rand::float_nd_array (dims, a(0)); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
313 else |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
314 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14706
diff
changeset
|
315 if (a.dims () != dims) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
316 error ("%s: mismatch in argument size", fcn); |
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
317 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
318 octave_idx_type len = a.numel (); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
319 FloatNDArray m (dims); |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
320 float *v = m.fortran_vec (); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
321 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
322 for (octave_idx_type i = 0; i < len; i++) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
323 v[i] = octave::rand::float_scalar (a(i)); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
324 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
325 return m; |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
326 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10066
diff
changeset
|
327 } |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
328 else |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
329 return octave::rand::float_nd_array (dims); |
5730 | 330 } |
331 else | |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
332 { |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
333 if (additional_arg) |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
334 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
335 if (a.numel () == 1) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
336 return octave::rand::nd_array (dims, a(0)); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
337 else |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
338 { |
14846
460a3c6d8bf1
maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents:
14706
diff
changeset
|
339 if (a.dims () != dims) |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
340 error ("%s: mismatch in argument size", fcn); |
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20801
diff
changeset
|
341 |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
20228
diff
changeset
|
342 octave_idx_type len = a.numel (); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
343 NDArray m (dims); |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
344 double *v = m.fortran_vec (); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
345 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
346 for (octave_idx_type i = 0; i < len; i++) |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
347 v[i] = octave::rand::scalar (a(i)); |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
348 |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
349 return m; |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
350 } |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
351 } |
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
352 else |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
353 return octave::rand::nd_array (dims); |
14655
43db83eff9db
Implement single precision rand, randn, rande, randg and randp generators (bug #34351, #36293)
David Bateman <dbateman@free.fr>
parents:
14620
diff
changeset
|
354 } |
2928 | 355 } |
356 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
357 DEFUN (rand, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
358 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
359 @deftypefn {} {} rand (@var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
360 @deftypefnx {} {} rand (@var{m}, @var{n}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
361 @deftypefnx {} {} rand ([@var{m} @var{n} @dots{}]) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
362 @deftypefnx {} {@var{v} =} rand ("state") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
363 @deftypefnx {} {} rand ("state", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
364 @deftypefnx {} {} rand ("state", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
365 @deftypefnx {} {@var{v} =} rand ("seed") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
366 @deftypefnx {} {} rand ("seed", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
367 @deftypefnx {} {} rand ("seed", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
368 @deftypefnx {} {} rand (@dots{}, "single") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
369 @deftypefnx {} {} rand (@dots{}, "double") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
370 Return a matrix with random elements uniformly distributed on the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
371 interval (0, 1). |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
372 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
373 The arguments are handled the same as the arguments for @code{eye}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
374 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
375 You can query the state of the random number generator using the form |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
376 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
377 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
378 v = rand ("state") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
379 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
380 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
381 This returns a column vector @var{v} of length 625. Later, you can restore |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
382 the random number generator to the state @var{v} using the form |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
383 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
384 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
385 rand ("state", v) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
386 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
387 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
388 @noindent |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
389 You may also initialize the state vector from an arbitrary vector of length |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
390 @leq{} 625 for @var{v}. This new state will be a hash based on the value of |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
391 @var{v}, not @var{v} itself. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
392 |
28598
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
393 By default, the generator is initialized by contributing entropy from the |
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
394 wall clock time, the CPU time, the current fraction of a second, the process |
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
395 ID and---if available---up to 1024 bits from the C++ random numbers source |
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
396 @code{random_device}, which might be non-deterministic (implementation |
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
397 specific). Note that this differs from @sc{matlab}, which always initializes |
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
398 the state to the same state at startup. To obtain behavior comparable to |
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
399 @sc{matlab}, initialize with a deterministic state vector in Octave's startup |
43c285a20829
Update documentation of the random number generator (bug #58800).
Markus Meisinger <chloros2@gmx.de>
parents:
27923
diff
changeset
|
400 files (@pxref{Startup Files}). |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
401 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
402 To compute the pseudo-random sequence, @code{rand} uses the Mersenne |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
403 Twister with a period of @math{2^{19937}-1} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
404 (See @nospell{M. Matsumoto and T. Nishimura}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
405 @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
406 pseudorandom number generator}, |
27800
5a6a19a4e3da
doc: Use Texinfo non-sentence ending periods in citations.
Rik <rik@octave.org>
parents:
27277
diff
changeset
|
407 @nospell{ACM} Trans.@: on Modeling and Computer Simulation Vol.@: 8, No.@: 1, |
5a6a19a4e3da
doc: Use Texinfo non-sentence ending periods in citations.
Rik <rik@octave.org>
parents:
27277
diff
changeset
|
408 pp.@: 3--30, January 1998, |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
409 @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}). |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
410 Do @strong{not} use for cryptography without securely hashing several |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
411 returned values together, otherwise the generator state can be learned after |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
412 reading 624 consecutive values. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
413 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
414 Older versions of Octave used a different random number generator. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
415 The new generator is used by default as it is significantly faster than the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
416 old generator, and produces random numbers with a significantly longer cycle |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
417 time. However, in some circumstances it might be desirable to obtain the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
418 same random sequences as produced by the old generators. To do this the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
419 keyword @qcode{"seed"} is used to specify that the old generators should |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
420 be used, as in |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
421 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
422 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
423 rand ("seed", val) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
424 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
425 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
426 @noindent |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
427 which sets the seed of the generator to @var{val}. The seed of the |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
428 generator can be queried with |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
429 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
430 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
431 s = rand ("seed") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
432 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
433 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
434 However, it should be noted that querying the seed will not cause |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
435 @code{rand} to use the old generators, only setting the seed will. To cause |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
436 @code{rand} to once again use the new generators, the keyword |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
437 @qcode{"state"} should be used to reset the state of the @code{rand}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
438 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
439 The state or seed of the generator can be reset to a new random value using |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
440 the @qcode{"reset"} keyword. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
441 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
442 The class of the value returned can be controlled by a trailing |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
443 @qcode{"double"} or @qcode{"single"} argument. These are the only valid |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
444 classes. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
445 @seealso{randn, rande, randg, randp} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
446 @end deftypefn */) |
2928 | 447 { |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
448 return do_rand (args, args.length (), "rand", "uniform"); |
2928 | 449 } |
450 | |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
451 // FIXME: The old generator (selected when "seed" is set) will not |
8871 | 452 // work properly if compiled to use 64-bit integers. |
453 | |
5730 | 454 /* |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
455 %!test # "state" can be a scalar |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
456 %! rand ("state", 12); x = rand (1,4); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
457 %! rand ("state", 12); y = rand (1,4); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
458 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
459 %!test # "state" can be a vector |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
460 %! rand ("state", [12,13]); x = rand (1,4); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
461 %! rand ("state", [12;13]); y = rand (1,4); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
462 %! assert (x, y); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
463 %!test # querying "state" returns a value which can be used later |
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
464 %! s = rand ("state"); x = rand (1,2); |
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
465 %! rand ("state", s); y = rand (1,2); |
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
466 %! assert (x, y); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
467 %!test # querying "state" doesn't disturb sequence |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
468 %! rand ("state", 12); rand (1,2); x = rand (1,2); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
469 %! rand ("state", 12); rand (1,2); s = rand ("state"); y = rand (1,2); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
470 %! assert (x, y); |
24816
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
471 %! rand ("state", s); z = rand (1,2); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
472 %! assert (x, z); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
473 %!test # "seed" must be a scalar |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
474 %! rand ("seed", 12); x = rand (1,4); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
475 %! rand ("seed", 12); y = rand (1,4); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
476 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
477 %!error <seed must be a real scalar> rand ("seed", [12,13]) |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
478 %!test # querying "seed" returns a value which can be used later |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
479 %! s = rand ("seed"); x = rand (1,2); |
24816
261d23b8933b
test: ensure all unit tests restore global random state after setting it
Mike Miller <mtmiller@octave.org>
parents:
24106
diff
changeset
|
480 %! rand ("seed", s); y = rand (1,2); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
481 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
482 %!test # querying "seed" doesn't disturb sequence |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
483 %! rand ("seed", 12); rand (1,2); x = rand (1,2); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
484 %! rand ("seed", 12); rand (1,2); s = rand ("seed"); y = rand (1,2); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
485 %! assert (x, y); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
486 %! rand ("seed", s); z = rand (1,2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
487 %! assert (x, z); |
5730 | 488 */ |
489 | |
490 /* | |
491 %!test | |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
492 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
493 %! rand ("state", 1); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
494 %! assert (rand (1,6), [0.1343642441124013 0.8474337369372327 0.763774618976614 0.2550690257394218 0.495435087091941 0.4494910647887382], eps); |
6437 | 495 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
496 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
497 %! rand ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
498 %! assert (rand (1,6), [0.8668024251237512 0.9126510815694928 0.09366085007786751 0.1664607301354408 0.7408077004365623 0.7615650338120759], 1e-6); |
5730 | 499 %!test |
6437 | 500 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
501 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
502 %! rand ("state", 12); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
503 %! x = rand (100_000, 1); |
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
504 %! assert (min (x) > 0); #*** Please report this!!! *** |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
505 %! assert (max (x) < 1); #*** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
506 %! assert (mean (x), 0.5, 0.0024); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
507 %! assert (var (x), 1/48, 0.0632); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
508 %! assert (skewness (x), 0, 0.012); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
509 %! assert (kurtosis (x), -6/5, 0.0094); |
6437 | 510 %! endif |
511 %!test | |
512 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
513 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
514 %! rand ("seed", 12); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
515 %! x = rand (100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
516 %! assert (max (x) < 1); #*** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
517 %! assert (min (x) > 0); #*** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
518 %! assert (mean (x), 0.5, 0.0024); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
519 %! assert (var (x), 1/48, 0.0632); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
520 %! assert (skewness (x), 0, 0.012); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
521 %! assert (kurtosis (x), -6/5, 0.0094); |
6437 | 522 %! endif |
5730 | 523 */ |
524 | |
17258
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
525 /* |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
526 ## Test out-of-range values as rand() seeds. |
17258
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
527 %!function v = __rand_sample__ (initval) |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
528 %! rand ("state", initval); |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
529 %! v = rand (1, 6); |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
530 %!endfunction |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
531 %! |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
532 %!assert (__rand_sample__ (-1), __rand_sample__ (0)) |
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
533 %!assert (__rand_sample__ (-Inf), __rand_sample__ (0)) |
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
534 %!assert (__rand_sample__ (2^33), __rand_sample__ (intmax ("uint32"))) |
25651
c759aa39c23a
Produce same random number stream for "state" initialized to Inf (bug #54342).
Rik <rik@octave.org>
parents:
25143
diff
changeset
|
535 %!assert (__rand_sample__ (Inf), __rand_sample__ (0)) |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
536 %!assert (__rand_sample__ (NaN), __rand_sample__ (0)) |
17258
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
537 */ |
213ee68b59da
Handle out-of-range values consistently when initializing random number generator.
Philipp Kutin <philipp.kutin@gmail.com>
parents:
15195
diff
changeset
|
538 |
25926
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
539 /* |
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
540 ## Check that negative dimensions are treated as zero for Matlab compatibility |
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
541 %!assert (size (rand (1, -1, 2)), [1, 0, 2]) |
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
542 |
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
543 ## Test input validation |
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
544 %!error <conversion of 1.1 to.* failed> rand (1, 1.1) |
25927
d6581134daaa
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension in a dimension vector (bug #54781).
Rik <rik@octave.org>
parents:
25926
diff
changeset
|
545 %!error <dimensions must be .* array of integers> rand ([1, 1.1]) |
25926
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
546 */ |
373fe1608f7c
Emit an error when utility matrices (zeros, ones, rand, etc.) are given a fractional dimension (bug #54781).
Rik <rik@octave.org>
parents:
25828
diff
changeset
|
547 |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
548 static std::string current_distribution = octave::rand::distribution (); |
4307 | 549 |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
550 DEFUN (randn, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
551 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
552 @deftypefn {} {} randn (@var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
553 @deftypefnx {} {} randn (@var{m}, @var{n}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
554 @deftypefnx {} {} randn ([@var{m} @var{n} @dots{}]) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
555 @deftypefnx {} {@var{v} =} randn ("state") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
556 @deftypefnx {} {} randn ("state", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
557 @deftypefnx {} {} randn ("state", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
558 @deftypefnx {} {@var{v} =} randn ("seed") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
559 @deftypefnx {} {} randn ("seed", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
560 @deftypefnx {} {} randn ("seed", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
561 @deftypefnx {} {} randn (@dots{}, "single") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
562 @deftypefnx {} {} randn (@dots{}, "double") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
563 Return a matrix with normally distributed random elements having zero mean |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
564 and variance one. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
565 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
566 The arguments are handled the same as the arguments for @code{rand}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
567 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
568 By default, @code{randn} uses the @nospell{Marsaglia and Tsang} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
569 ``Ziggurat technique'' to transform from a uniform to a normal distribution. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
570 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
571 The class of the value returned can be controlled by a trailing |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
572 @qcode{"double"} or @qcode{"single"} argument. These are the only valid |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
573 classes. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
574 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
575 Reference: @nospell{G. Marsaglia and W.W. Tsang}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
576 @cite{Ziggurat Method for Generating Random Variables}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
577 J. Statistical Software, vol 5, 2000, |
25143
13fd0610480f
doc: Use https whenever possible in @url entries.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
578 @url{https://www.jstatsoft.org/v05/i08/} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
579 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
580 @seealso{rand, rande, randg, randp} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
581 @end deftypefn */) |
2928 | 582 { |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
583 return do_rand (args, args.length (), "randn", "normal"); |
2928 | 584 } |
585 | |
586 /* | |
5730 | 587 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
588 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
589 %! randn ("state", 1); |
24989
15d2f32db174
test: relax tolerances on randn, randg tests to pass on i386 (bug #53438)
Mike Miller <mtmiller@octave.org>
parents:
24856
diff
changeset
|
590 %! assert (randn (1, 6), [-2.666521678978671 -0.7381719971724564 1.507903992673601 0.6019427189162239 -0.450661261143348 -0.7054431351574116], 14*eps); |
6437 | 591 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
592 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
593 %! randn ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
594 %! assert (randn (1, 6), [-1.039402365684509 -1.25938892364502 0.1968704611063004 0.3874166905879974 -0.5976632833480835 -0.6615074276924133], 1e-6); |
5730 | 595 %!test |
6437 | 596 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
597 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
598 %! randn ("state", 12); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
599 %! x = randn (100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
600 %! assert (mean (x), 0, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
601 %! assert (var (x), 1, 0.02); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
602 %! assert (skewness (x), 0, 0.02); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
603 %! assert (kurtosis (x), 0, 0.04); |
6437 | 604 %! endif |
605 %!test | |
606 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
607 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
608 %! randn ("seed", 12); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
609 %! x = randn (100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
610 %! assert (mean (x), 0, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
611 %! assert (var (x), 1, 0.02); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
612 %! assert (skewness (x), 0, 0.02); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
613 %! assert (kurtosis (x), 0, 0.04); |
6437 | 614 %! endif |
5730 | 615 */ |
616 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
617 DEFUN (rande, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
618 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
619 @deftypefn {} {} rande (@var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
620 @deftypefnx {} {} rande (@var{m}, @var{n}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
621 @deftypefnx {} {} rande ([@var{m} @var{n} @dots{}]) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
622 @deftypefnx {} {@var{v} =} rande ("state") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
623 @deftypefnx {} {} rande ("state", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
624 @deftypefnx {} {} rande ("state", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
625 @deftypefnx {} {@var{v} =} rande ("seed") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
626 @deftypefnx {} {} rande ("seed", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
627 @deftypefnx {} {} rande ("seed", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
628 @deftypefnx {} {} rande (@dots{}, "single") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
629 @deftypefnx {} {} rande (@dots{}, "double") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
630 Return a matrix with exponentially distributed random elements. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
631 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
632 The arguments are handled the same as the arguments for @code{rand}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
633 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
634 By default, @code{rande} uses the @nospell{Marsaglia and Tsang} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
635 ``Ziggurat technique'' to transform from a uniform to an exponential |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
636 distribution. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
637 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
638 The class of the value returned can be controlled by a trailing |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
639 @qcode{"double"} or @qcode{"single"} argument. These are the only valid |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
640 classes. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
641 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
642 Reference: @nospell{G. Marsaglia and W.W. Tsang}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
643 @cite{Ziggurat Method for Generating Random Variables}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
644 J. Statistical Software, vol 5, 2000, |
25143
13fd0610480f
doc: Use https whenever possible in @url entries.
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
645 @url{https://www.jstatsoft.org/v05/i08/} |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
646 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
647 @seealso{rand, randn, randg, randp} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
648 @end deftypefn */) |
5730 | 649 { |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
650 return do_rand (args, args.length (), "rande", "exponential"); |
5730 | 651 } |
652 | |
653 /* | |
654 %!test | |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
655 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
656 %! rande ("state", 1); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
657 %! assert (rande (1, 6), [3.602973885835625 0.1386190677555021 0.6743112889616958 0.4512830847258422 0.7255744741233175 0.3415969205292291], 2*eps); |
6437 | 658 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
659 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
660 %! rande ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
661 %! assert (rande (1, 6), [0.06492075175653866 1.717980206012726 0.4816154008731246 0.5231300676241517 0.103910739364359 1.668931916356087], 1e-6); |
5730 | 662 %!test |
6437 | 663 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
664 %! ## statistical tests may fail occasionally |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
665 %! rande ("state", 1); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
666 %! x = rande (100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
667 %! assert (min (x) > 0); # *** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
668 %! assert (mean (x), 1, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
669 %! assert (var (x), 1, 0.03); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
670 %! assert (skewness (x), 2, 0.06); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
671 %! assert (kurtosis (x), 6, 0.7); |
6437 | 672 %! endif |
673 %!test | |
674 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
675 %! ## statistical tests may fail occasionally |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
676 %! rande ("seed", 1); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
677 %! x = rande (100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
678 %! assert (min (x)>0); # *** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
679 %! assert (mean (x), 1, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
680 %! assert (var (x), 1, 0.03); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
681 %! assert (skewness (x), 2, 0.06); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
682 %! assert (kurtosis (x), 6, 0.7); |
6437 | 683 %! endif |
5730 | 684 */ |
685 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
686 DEFUN (randg, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
687 doc: /* -*- texinfo -*- |
24106
a6cf8e06603a
doc: Correct documentation for randg (bug #52118).
Rik <rik@octave.org>
parents:
23219
diff
changeset
|
688 @deftypefn {} {} randg (@var{a}, @var{n}) |
a6cf8e06603a
doc: Correct documentation for randg (bug #52118).
Rik <rik@octave.org>
parents:
23219
diff
changeset
|
689 @deftypefnx {} {} randg (@var{a}, @var{m}, @var{n}, @dots{}) |
a6cf8e06603a
doc: Correct documentation for randg (bug #52118).
Rik <rik@octave.org>
parents:
23219
diff
changeset
|
690 @deftypefnx {} {} randg (@var{a}, [@var{m} @var{n} @dots{}]) |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
691 @deftypefnx {} {@var{v} =} randg ("state") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
692 @deftypefnx {} {} randg ("state", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
693 @deftypefnx {} {} randg ("state", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
694 @deftypefnx {} {@var{v} =} randg ("seed") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
695 @deftypefnx {} {} randg ("seed", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
696 @deftypefnx {} {} randg ("seed", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
697 @deftypefnx {} {} randg (@dots{}, "single") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
698 @deftypefnx {} {} randg (@dots{}, "double") |
24106
a6cf8e06603a
doc: Correct documentation for randg (bug #52118).
Rik <rik@octave.org>
parents:
23219
diff
changeset
|
699 |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
700 Return a matrix with @code{gamma (@var{a},1)} distributed random elements. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
701 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
702 The arguments are handled the same as the arguments for @code{rand}, except |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
703 for the argument @var{a}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
704 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
705 This can be used to generate many distributions: |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
706 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
707 @table @asis |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
708 @item @code{gamma (a, b)} for @code{a > -1}, @code{b > 0} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
709 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
710 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
711 r = b * randg (a) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
712 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
713 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
714 @item @code{beta (a, b)} for @code{a > -1}, @code{b > -1} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
715 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
716 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
717 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
718 r1 = randg (a, 1) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
719 r = r1 / (r1 + randg (b, 1)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
720 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
721 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
722 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
723 @item @code{Erlang (a, n)} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
724 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
725 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
726 r = a * randg (n) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
727 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
728 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
729 @item @code{chisq (df)} for @code{df > 0} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
730 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
731 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
732 r = 2 * randg (df / 2) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
733 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
734 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
735 @item @code{t (df)} for @code{0 < df < inf} (use randn if df is infinite) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
736 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
737 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
738 r = randn () / sqrt (2 * randg (df / 2) / df) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
739 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
740 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
741 @item @code{F (n1, n2)} for @code{0 < n1}, @code{0 < n2} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
742 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
743 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
744 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
745 ## r1 equals 1 if n1 is infinite |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
746 r1 = 2 * randg (n1 / 2) / n1 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
747 ## r2 equals 1 if n2 is infinite |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
748 r2 = 2 * randg (n2 / 2) / n2 |
21968
973db845cb43
doc: Remove stray '\n' from docstrings.
Rik <rik@octave.org>
parents:
21966
diff
changeset
|
749 r = r1 / r2 |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
750 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
751 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
752 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
753 @item negative @code{binomial (n, p)} for @code{n > 0}, @code{0 < p <= 1} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
754 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
755 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
756 r = randp ((1 - p) / p * randg (n)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
757 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
758 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
759 @item non-central @code{chisq (df, L)}, for @code{df >= 0} and @code{L > 0} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
760 (use chisq if @code{L = 0}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
761 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
762 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
763 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
764 r = randp (L / 2) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
765 r(r > 0) = 2 * randg (r(r > 0)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
766 r(df > 0) += 2 * randg (df(df > 0)/2) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
767 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
768 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
769 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
770 @item @code{Dirichlet (a1, @dots{} ak)} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
771 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
772 @example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
773 @group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
774 r = (randg (a1), @dots{}, randg (ak)) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
775 r = r / sum (r) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
776 @end group |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
777 @end example |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
778 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
779 @end table |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
780 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
781 The class of the value returned can be controlled by a trailing |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
782 @qcode{"double"} or @qcode{"single"} argument. These are the only valid |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
783 classes. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
784 @seealso{rand, randn, rande, randp} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
785 @end deftypefn */) |
5730 | 786 { |
787 int nargin = args.length (); | |
788 | |
789 if (nargin < 1) | |
790 error ("randg: insufficient arguments"); | |
791 | |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
792 return do_rand (args, nargin, "randg", "gamma", true); |
5730 | 793 } |
794 | |
795 /* | |
796 %!test | |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
797 %! randg ("state", 12); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
798 %! assert (randg ([-inf, -1, 0, inf, nan]), [nan, nan, nan, nan, nan]); # *** Please report |
6437 | 799 |
800 %!test | |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
801 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
802 %! randg ("state", 1); |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
803 %! assert (randg (0.1, 1, 6), [0.0103951513331241 8.335671459898252e-05 0.00138691397249762 0.000587308416993855 0.495590518784736 2.3921917414795e-12], eps); |
6437 | 804 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
805 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
806 %! randg ("state", 1); |
24989
15d2f32db174
test: relax tolerances on randn, randg tests to pass on i386 (bug #53438)
Mike Miller <mtmiller@octave.org>
parents:
24856
diff
changeset
|
807 %! assert (randg (0.95, 1, 6), [3.099382433255327 0.3974529788871218 0.644367450750855 1.143261091802246 1.964111762696822 0.04011915547957939], 12*eps); |
6437 | 808 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
809 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
810 %! randg ("state", 1); |
24989
15d2f32db174
test: relax tolerances on randn, randg tests to pass on i386 (bug #53438)
Mike Miller <mtmiller@octave.org>
parents:
24856
diff
changeset
|
811 %! assert (randg (1, 1, 6), [0.2273389379645993 1.288822625058359 0.2406335209340746 1.218869553370733 1.024649860162554 0.09631230343599533], 40*eps); |
6437 | 812 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
813 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
814 %! randg ("state", 1); |
24989
15d2f32db174
test: relax tolerances on randn, randg tests to pass on i386 (bug #53438)
Mike Miller <mtmiller@octave.org>
parents:
24856
diff
changeset
|
815 %! assert (randg (10, 1, 6), [3.520369644331133 15.15369864472106 8.332112081991205 8.406211067432674 11.81193475187611 10.88792728177059], 56*eps); |
6437 | 816 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
817 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
818 %! randg ("state", 1); |
24989
15d2f32db174
test: relax tolerances on randn, randg tests to pass on i386 (bug #53438)
Mike Miller <mtmiller@octave.org>
parents:
24856
diff
changeset
|
819 %! assert (randg (100, 1, 6), [75.34570255262264 115.4911985594699 95.23493031356388 95.48926019250911 106.2397448229803 103.4813150404118], 256*eps); |
6437 | 820 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
821 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
822 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
823 %! assert (randg (0.1, 1, 6), [0.07144210487604141 0.460641473531723 0.4749028384685516 0.06823389977216721 0.000293838675133884 1.802567535340305e-12], 1e-6); |
6437 | 824 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
825 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
826 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
827 %! assert (randg (0.95, 1, 6), [1.664905071258545 1.879976987838745 1.905677795410156 0.9948706030845642 0.5606933236122131 0.0766092911362648], 1e-6); |
6437 | 828 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
829 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
830 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
831 %! assert (randg (1, 1, 6), [0.03512085229158401 0.6488978862762451 0.8114678859710693 0.1666885763406754 1.60791552066803 1.90356981754303], 1e-6); |
6437 | 832 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
833 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
834 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
835 %! assert (randg (10, 1, 6), [6.566435813903809 10.11648464202881 10.73162078857422 7.747178077697754 6.278522491455078 6.240195751190186], 1e-5); |
6437 | 836 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
837 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
838 %! randg ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
839 %! assert (randg (100, 1, 6), [89.40208435058594 101.4734725952148 103.4020004272461 93.62763214111328 88.33104705810547 88.1871337890625], 1e-4); |
18573
53ba52143af7
Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents:
18436
diff
changeset
|
840 %!test |
53ba52143af7
Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents:
18436
diff
changeset
|
841 %! ## Test out-of-bounds values produce NaN w/old-style generators & floats |
53ba52143af7
Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents:
18436
diff
changeset
|
842 %! randg ("seed", 1); |
53ba52143af7
Fix dead assignment in oct-rand.cc do_float_scalar (bug #41827).
Branden Archer <b.m.archer4@gmail.com>
parents:
18436
diff
changeset
|
843 %! result = randg ([-2 Inf], "single"); |
18574
756e906bdfda
Fix %!test comparing NaN (single) to NaN (double) in cset 53ba52143af7.
Rik <rik@octave.org>
parents:
18573
diff
changeset
|
844 %! assert (result, single ([NaN NaN])); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
845 |
6437 | 846 %!test |
847 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
848 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
849 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
850 %! a = 0.1; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
851 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
852 %! assert (mean (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
853 %! assert (var (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
854 %! assert (skewness (x), 2/sqrt (a), 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
855 %! assert (kurtosis (x), 6/a, 50); |
6437 | 856 %! endif |
857 %!test | |
858 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
859 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
860 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
861 %! a = 0.95; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
862 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
863 %! assert (mean (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
864 %! assert (var (x), a, 0.04); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
865 %! assert (skewness (x), 2/sqrt (a), 0.2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
866 %! assert (kurtosis (x), 6/a, 2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
867 %! endif |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
868 %!test |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
869 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
870 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
871 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
872 %! a = 1; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
873 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
874 %! assert (mean (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
875 %! assert (var (x), a, 0.04); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
876 %! assert (skewness (x), 2/sqrt (a), 0.2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
877 %! assert (kurtosis (x), 6/a, 2); |
6437 | 878 %! endif |
879 %!test | |
880 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
881 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
882 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
883 %! a = 10; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
884 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
885 %! assert (mean (x), a, 0.1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
886 %! assert (var (x), a, 0.5); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
887 %! assert (skewness (x), 2/sqrt (a), 0.1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
888 %! assert (kurtosis (x), 6/a, 0.5); |
6437 | 889 %! endif |
890 %!test | |
891 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
892 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
893 %! randg ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
894 %! a = 100; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
895 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
896 %! assert (mean (x), a, 0.2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
897 %! assert (var (x), a, 2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
898 %! assert (skewness (x), 2/sqrt (a), 0.05); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
899 %! assert (kurtosis (x), 6/a, 0.2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
900 %! endif |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
901 %!test |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
902 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
903 %!assert (randg ([-inf, -1, 0, inf, nan]), [nan, nan, nan, nan, nan]) # *** Please report |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
904 %!test |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
905 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
906 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
907 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
908 %! a = 0.1; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
909 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
910 %! assert (mean (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
911 %! assert (var (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
912 %! assert (skewness (x), 2/sqrt (a), 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
913 %! assert (kurtosis (x), 6/a, 50); |
6437 | 914 %! endif |
915 %!test | |
916 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
917 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
918 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
919 %! a = 0.95; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
920 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
921 %! assert (mean (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
922 %! assert (var (x), a, 0.04); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
923 %! assert (skewness (x), 2/sqrt (a), 0.2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
924 %! assert (kurtosis (x), 6/a, 2); |
6437 | 925 %! endif |
926 %!test | |
927 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
928 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
929 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
930 %! a = 1; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
931 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
932 %! assert (mean (x), a, 0.01); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
933 %! assert (var (x), a, 0.04); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
934 %! assert (skewness (x), 2/sqrt (a), 0.2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
935 %! assert (kurtosis (x), 6/a, 2); |
6437 | 936 %! endif |
5730 | 937 %!test |
6437 | 938 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
939 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
940 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
941 %! a = 10; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
942 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
943 %! assert (mean (x), a, 0.1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
944 %! assert (var (x), a, 0.5); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
945 %! assert (skewness (x), 2/sqrt (a), 0.1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
946 %! assert (kurtosis (x), 6/a, 0.5); |
6437 | 947 %! endif |
5730 | 948 %!test |
6437 | 949 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
950 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
951 %! randg ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
952 %! a = 100; |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
953 %! x = randg (a, 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
954 %! assert (mean (x), a, 0.2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
955 %! assert (var (x), a, 2); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
956 %! assert (skewness (x), 2/sqrt (a), 0.05); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
957 %! assert (kurtosis (x), 6/a, 0.2); |
6437 | 958 %! endif |
5730 | 959 */ |
960 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
961 DEFUN (randp, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
962 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
963 @deftypefn {} {} randp (@var{l}, @var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
964 @deftypefnx {} {} randp (@var{l}, @var{m}, @var{n}, @dots{}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
965 @deftypefnx {} {} randp (@var{l}, [@var{m} @var{n} @dots{}]) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
966 @deftypefnx {} {@var{v} =} randp ("state") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
967 @deftypefnx {} {} randp ("state", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
968 @deftypefnx {} {} randp ("state", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
969 @deftypefnx {} {@var{v} =} randp ("seed") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
970 @deftypefnx {} {} randp ("seed", @var{v}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
971 @deftypefnx {} {} randp ("seed", "reset") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
972 @deftypefnx {} {} randp (@dots{}, "single") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
973 @deftypefnx {} {} randp (@dots{}, "double") |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
974 Return a matrix with Poisson distributed random elements with mean value |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
975 parameter given by the first argument, @var{l}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
976 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
977 The arguments are handled the same as the arguments for @code{rand}, except |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
978 for the argument @var{l}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
979 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
980 Five different algorithms are used depending on the range of @var{l} and |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
981 whether or not @var{l} is a scalar or a matrix. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
982 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
983 @table @asis |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
984 @item For scalar @var{l} @leq{} 12, use direct method. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
985 W.H. Press, et al., @cite{Numerical Recipes in C}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
986 Cambridge University Press, 1992. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
987 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
988 @item For scalar @var{l} > 12, use rejection method.[1] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
989 W.H. Press, et al., @cite{Numerical Recipes in C}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
990 Cambridge University Press, 1992. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
991 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
992 @item For matrix @var{l} @leq{} 10, use inversion method.[2] |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
993 @nospell{E. Stadlober, et al., WinRand source code}, available via FTP. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
994 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
995 @item For matrix @var{l} > 10, use patchwork rejection method. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
996 @nospell{E. Stadlober, et al., WinRand source code}, available via FTP, or |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
997 @nospell{H. Zechner}, @cite{Efficient sampling from continuous and discrete |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
998 unimodal distributions}, Doctoral Dissertation, 156pp., Technical |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
999 University @nospell{Graz}, Austria, 1994. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1000 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1001 @item For @var{l} > 1e8, use normal approximation. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1002 @nospell{L. Montanet}, et al., @cite{Review of Particle Properties}, |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1003 Physical Review D 50 p1284, 1994. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1004 @end table |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1005 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1006 The class of the value returned can be controlled by a trailing |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1007 @qcode{"double"} or @qcode{"single"} argument. These are the only valid |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1008 classes. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1009 @seealso{rand, randn, rande, randg} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1010 @end deftypefn */) |
5730 | 1011 { |
1012 int nargin = args.length (); | |
1013 | |
1014 if (nargin < 1) | |
1015 error ("randp: insufficient arguments"); | |
1016 | |
20894
e528d7ab1cad
rand.cc: Overhaul file to make use of new C++ archetype.
Rik <rik@octave.org>
parents:
20853
diff
changeset
|
1017 return do_rand (args, nargin, "randp", "poisson", true); |
5730 | 1018 } |
1019 | |
1020 /* | |
1021 %!test | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1022 %! randp ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1023 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report |
6437 | 1024 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1025 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1026 %! randp ("state", 1); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
1027 %! assert (randp (5, 1, 6), [5 5 3 7 7 3]); |
6437 | 1028 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1029 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1030 %! randp ("state", 1); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
1031 %! assert (randp (15, 1, 6), [13 15 8 18 18 15]); |
6437 | 1032 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1033 %! ## Test a known fixed state |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1034 %! randp ("state", 1); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
1035 %! assert (randp (1e9, 1, 6), [999915677 999976657 1000047684 1000019035 999985749 999977692], -1e-6); |
6437 | 1036 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1037 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1038 %! randp ("seed", 1); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1039 %! %%assert (randp (5, 1, 6), [8 2 3 6 6 8]) |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
1040 %! assert (randp (5, 1, 5), [8 2 3 6 6]); |
6437 | 1041 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1042 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1043 %! randp ("seed", 1); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
1044 %! assert (randp (15, 1, 6), [15 16 12 10 10 12]); |
6437 | 1045 %!test |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1046 %! ## Test a known fixed seed |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1047 %! randp ("seed", 1); |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21301
diff
changeset
|
1048 %! assert (randp (1e9, 1, 6), [1000006208 1000012224 999981120 999963520 999963072 999981440], -1e-6); |
6437 | 1049 %!test |
1050 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1051 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1052 %! randp ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1053 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1054 %! x = randp (a (1), 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1055 %! assert (min (x) >= 0); # *** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1056 %! assert (mean (x), a(1), a(2)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1057 %! assert (var (x), a(1), 0.02*a(1)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1058 %! assert (skewness (x), 1/sqrt (a(1)), a(3)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1059 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6437 | 1060 %! endfor |
1061 %! endif | |
1062 %!test | |
1063 %! if (__random_statistical_tests__) | |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1064 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1065 %! randp ("state", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1066 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1067 %! x = randp (a(1)*ones (100_000, 1), 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1068 %! assert (min (x) >= 0); # *** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1069 %! assert (mean (x), a(1), a(2)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1070 %! assert (var (x), a(1), 0.02*a(1)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1071 %! assert (skewness (x), 1/sqrt (a(1)), a(3)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1072 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6437 | 1073 %! endfor |
1074 %! endif | |
1075 %!test | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1076 %! randp ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1077 %! assert (randp ([-inf, -1, 0, inf, nan]), [nan, nan, 0, nan, nan]); # *** Please report |
5730 | 1078 %!test |
6449 | 1079 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1080 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1081 %! randp ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1082 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1083 %! x = randp (a(1), 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1084 %! assert (min (x) >= 0); # *** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1085 %! assert (mean (x), a(1), a(2)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1086 %! assert (var (x), a(1), 0.02*a(1)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1087 %! assert (skewness (x), 1/sqrt (a(1)), a(3)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1088 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6449 | 1089 %! endfor |
1090 %! endif | |
5730 | 1091 %!test |
6449 | 1092 %! if (__random_statistical_tests__) |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
1093 %! ## statistical tests may fail occasionally. |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1094 %! randp ("seed", 12); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1095 %! for a = [5, 15, 1e9; 0.03, 0.03, -5e-3; 0.03, 0.03, 0.03] |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1096 %! x = randp (a(1)*ones (100_000, 1), 100_000, 1); |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1097 %! assert (min (x) >= 0); # *** Please report this!!! *** |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1098 %! assert (mean (x), a(1), a(2)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1099 %! assert (var (x), a(1), 0.02*a(1)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1100 %! assert (skewness (x), 1/sqrt (a(1)), a(3)); |
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
1101 %! assert (kurtosis (x), 1/a(1), 3*a(3)); |
6449 | 1102 %! endfor |
1103 %! endif | |
5730 | 1104 */ |
1105 | |
15039
e753177cde93
maint: Move non-dynamically linked functions from DLD-FUNCTIONS/ to corefcn/ directory
Rik <rik@octave.org>
parents:
14854
diff
changeset
|
1106 DEFUN (randperm, args, , |
21966
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1107 doc: /* -*- texinfo -*- |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1108 @deftypefn {} {} randperm (@var{n}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1109 @deftypefnx {} {} randperm (@var{n}, @var{m}) |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1110 Return a row vector containing a random permutation of @code{1:@var{n}}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1111 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1112 If @var{m} is supplied, return @var{m} unique entries, sampled without |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1113 replacement from @code{1:@var{n}}. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1114 |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1115 The complexity is O(@var{n}) in memory and O(@var{m}) in time, unless |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1116 @var{m} < @var{n}/5, in which case O(@var{m}) memory is used as well. The |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1117 randomization is performed using rand(). All permutations are equally |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1118 likely. |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1119 @seealso{perms} |
112b20240c87
move docstrings in C++ files out of C strings and into comments
John W. Eaton <jwe@octave.org>
parents:
21942
diff
changeset
|
1120 @end deftypefn */) |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1121 { |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1122 int nargin = args.length (); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1123 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1124 if (nargin < 1 || nargin > 2) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1125 print_usage (); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1126 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1127 octave_idx_type n = args(0).idx_type_value (true); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1128 octave_idx_type m = (nargin == 2) ? args(1).idx_type_value (true) : n; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
1129 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1130 if (m < 0 || n < 0) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1131 error ("randperm: M and N must be non-negative"); |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1132 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1133 if (m > n) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1134 error ("randperm: M must be less than or equal to N"); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1135 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1136 // Quick and dirty heuristic to decide if we allocate or not the |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1137 // whole vector for tracking the truncated shuffle. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1138 bool short_shuffle = m < n/5; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1139 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1140 // Generate random numbers. |
25433
49e0447413ad
use templates and move rand functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
25143
diff
changeset
|
1141 NDArray r = octave::rand::nd_array (dim_vector (1, m)); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1142 double *rvec = r.fortran_vec (); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1143 |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
1144 octave_idx_type idx_len = (short_shuffle ? m : n); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1145 Array<octave_idx_type> idx; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1146 try |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1147 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1148 idx = Array<octave_idx_type> (dim_vector (1, idx_len)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1149 } |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1150 catch (const std::bad_alloc&) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1151 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1152 // Looks like n is too big and short_shuffle is false. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1153 // Let's try again, but this time with the alternative. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1154 idx_len = m; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1155 short_shuffle = true; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1156 idx = Array<octave_idx_type> (dim_vector (1, idx_len)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1157 } |
13186
2896c083576a
Implement second randperm argument for compatibility with Matlab
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13026
diff
changeset
|
1158 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1159 octave_idx_type *ivec = idx.fortran_vec (); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1160 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1161 for (octave_idx_type i = 0; i < idx_len; i++) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1162 ivec[i] = i; |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1163 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1164 if (short_shuffle) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1165 { |
22150
c4901d7a2d7d
Remove check for std::tr1::unordered_map because we depend on C++11.
Carnë Draug <carandraug@octave.org>
parents:
21968
diff
changeset
|
1166 std::unordered_map<octave_idx_type, octave_idx_type> map (m); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1167 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1168 // Perform the Knuth shuffle only keeping track of moved |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1169 // entries in the map |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1170 for (octave_idx_type i = 0; i < m; i++) |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1171 { |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
1172 octave_idx_type k = i + std::floor (rvec[i] * (n - i)); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1173 |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
1174 // For shuffling first m entries, no need to use extra storage |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1175 if (k < m) |
17662
0b7f5c56f853
Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
17336
diff
changeset
|
1176 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1177 std::swap (ivec[i], ivec[k]); |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1178 } |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1179 else |
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1180 { |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1181 if (map.find (k) == map.end ()) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1182 map[k] = k; |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1183 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1184 std::swap (ivec[i], map[k]); |
13255
dd3c5325039c
Use a hash map to store permutations in randperm's truncated Knuth shuffle
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13186
diff
changeset
|
1185 } |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1186 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1187 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1188 else |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1189 { |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1190 // Perform the Knuth shuffle of the first m entries |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1191 for (octave_idx_type i = 0; i < m; i++) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1192 { |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
1193 octave_idx_type k = i + std::floor (rvec[i] * (n - i)); |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1194 std::swap (ivec[i], ivec[k]); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1195 } |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1196 } |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1197 |
20801
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1198 // Convert to doubles, reusing r. |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1199 for (octave_idx_type i = 0; i < m; i++) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1200 rvec[i] = ivec[i] + 1; |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1201 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1202 if (m < n) |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1203 idx.resize (dim_vector (1, m)); |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1204 |
a542a9bf177e
eliminate return statements after calls to print_usage
John W. Eaton <jwe@octave.org>
parents:
20785
diff
changeset
|
1205 // Now create an array object with a cached idx_vector. |
21127
df7891224709
maint: Use "return ovl (...)" in DEFUN macros.
Rik <rik@octave.org>
parents:
21100
diff
changeset
|
1206 return ovl (new octave_matrix (r, idx_vector (idx))); |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1207 } |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1208 |
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1209 /* |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1210 %!assert (sort (randperm (20)), 1:20) |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1211 %!assert (length (randperm (20,10)), 10) |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1212 |
22489
93ea313301f9
test: Add bug ids (<#####>) to BIST tests.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
1213 ## Test biggish N |
24856
8bb0251fcfde
Use a uint32 state vector for random number generators (bug #50256).
Rik <rik@octave.org>
parents:
24817
diff
changeset
|
1214 %!assert <*39378> (length (randperm (30_000^2, 100_000)), 100_000) |
17662
0b7f5c56f853
Allow randperm to work with bigger inputs (bug #39378)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
17336
diff
changeset
|
1215 |
14688
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1216 %!test |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1217 %! rand ("seed", 0); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1218 %! for i = 1:100 |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1219 %! p = randperm (305, 30); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1220 %! assert (length (unique (p)), 30); |
42edbe6f6a8d
Fix bad two-arg call of randperm (bug #36535)
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14138
diff
changeset
|
1221 %! endfor |
9647
54f45f883a53
optimize & extend randperm
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
1222 */ |