annotate scripts/general/rng.m @ 31202:434b5a1b9498

isequal.m: Add ability to compare Java objects (bug #62930) * isequal.m: Add elseif clause to detect Java objects and use built-in Java "equals" method for comparison. Add BIST test for Java object comparison.
author Rik <rik@octave.org>
date Thu, 25 Aug 2022 15:35:25 -0700
parents 5d3faba0342e
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
1 ########################################################################
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2020-2022 The Octave Project Developers
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
4 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
7 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
8 ## This file is part of Octave.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
9 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
11 ## under the terms of the GNU General Public License as published by
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
13 ## (at your option) any later version.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
14 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
18 ## GNU General Public License for more details.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
19 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
20 ## You should have received a copy of the GNU General Public License
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
22 ## <https://www.gnu.org/licenses/>.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
23 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
24 ########################################################################
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
25
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
26 ## -*- texinfo -*-
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
27 ## @deftypefn {} {} rng (@var{seed})
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
28 ## @deftypefnx {} {} rng (@var{seed}, "@var{generator}")
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
29 ## @deftypefnx {} {} rng ("shuffle")
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
30 ## @deftypefnx {} {} rng ("shuffle", "@var{generator}")
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
31 ## @deftypefnx {} {} rng ("default")
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
32 ## @deftypefnx {} {@var{s} =} rng ()
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
33 ## @deftypefnx {} {} rng (@var{s})
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
34 ## @deftypefnx {} {@var{s} =} rng (@dots{})
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
35 ## Set or query the seed of the random number generator used by @code{rand} and
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
36 ## @code{randn}.
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
37 ##
28957
efc998c08d27 doc: Use @var rather than @code to mark function inputs in TexInfo.
Rik <rik@octave.org>
parents: 28945
diff changeset
38 ## The input @var{seed} is a scalar numeric value used to initialize the state
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
39 ## vector of the random number generator.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
40 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
41 ## The optional string @var{generator} specifies the type of random number
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
42 ## generator to be used. Its value can be @qcode{"twister"},
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
43 ## @qcode{"v5uniform"}, or @qcode{"v5normal"}. The @qcode{"twister"} keyword
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
44 ## is described below. @qcode{"v5uniform"} and @qcode{"v5normal"} refer to
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
45 ## older versions of Octave that used to use a different random number
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
46 ## generator.
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
47 ##
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
48 ## The state or seed of the random number generator can be reset to a new
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
49 ## random value using the @qcode{"shuffle"} keyword.
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
50 ##
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
51 ## The random number generator can be reset to default values using the
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
52 ## @qcode{"default"} keyword. The default values are to use the Mersenne
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
53 ## Twister generator with a seed of 0.
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
54 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
55 ## The optional return value @var{s} contains the state of the random number
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
56 ## generator at the time the function is called (i.e., before it might be
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
57 ## modified according to the input arguments). It is encoded as a structure
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
58 ## variable with three fields: @qcode{"Type"}, @qcode{"Seed"}, and
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
59 ## @qcode{"State"}. The random number generator can be restored to the state
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
60 ## @var{s} using @code{rng (@var{s})}. This is useful when the identical
28693
2bb050267d74 maint: Remove trailing spaces from code base.
Rik <rik@octave.org>
parents: 28111
diff changeset
61 ## sequence of pseudo-random numbers is required for an algorithm.
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
62 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
63 ## By default, and with the @qcode{"twister"} option, pseudo-random sequences
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
64 ## are computed using the Mersenne Twister with a period of @math{2^{19937}-1}
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
65 ## (See @nospell{M. Matsumoto and T. Nishimura},
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
66 ## @cite{Mersenne Twister: A 623-dimensionally equidistributed uniform
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
67 ## pseudorandom number generator},
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
68 ## @nospell{ACM} Trans.@: on Modeling and Computer Simulation Vol.@: 8, No.@: 1,
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
69 ## pp.@: 3--30, January 1998,
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
70 ## @url{http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html}).
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
71 ## Do @strong{not} use for cryptography without securely hashing several
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
72 ## returned values together, otherwise the generator state can be learned after
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
73 ## reading 624 consecutive values.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
74 ##
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
75 ## @seealso{rand, randn}
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
76 ## @end deftypefn
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
77
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
78 function s = rng (varargin)
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
79
28107
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
80 if (nargin > 2)
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
81 print_usage ();
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
82 endif
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
83
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
84 ## Store current settings of random number generator
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
85 ## FIXME: there doesn't seem to be a way to query the type of generator
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
86 ## currently used in Octave - assume "twister".
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
87 ## FIXME: there doesn't seem to be a way to query the seed initialization
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
88 ## value - use "Not applicable".
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
89 ## FIXME: rand and randn use different generators - storing both states.
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
90 ## For older Matlab generators (v4, v5), the settings are stored like this:
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
91 ## struct ("Type","Legacy", "Seed", "Not applicable", "State",{[],[],...})
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
92
28107
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
93 ## Type is the generator name.
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
94 ## Seed is the initial seed value.
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
95 ## State is a structure describing internal state of the generator.
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
96 srng = struct ("Type", "twister",
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
97 "Seed", "Not applicable",
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
98 "State", {{rand("state"), randn("state")}});
28107
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
99
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
100 if (nargin == 0)
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
101 s = srng;
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
102 return;
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
103 endif
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
104
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
105 arg1 = varargin{1};
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
106 if (isscalar (arg1) && isnumeric (arg1) && isreal (arg1) && arg1 >= 0)
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
107 s_rand = s_randn = arg1;
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
108 generator = check_generator (varargin(2:end));
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
109
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
110 elseif (ischar (arg1) && strcmpi (arg1, "shuffle"))
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
111 ## Seed the random number generator based on the current time
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
112 s_rand = s_randn = "reset"; # or sum (1000*clock)
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
113 generator = check_generator (varargin(2:end));
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
114
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
115 elseif (ischar (arg1) && strcmpi (arg1, "default") && nargin == 1)
28107
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
116 generator = "twister";
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
117 s_rand = s_randn = 0; # FIXME: In Matlab, seed 0 corresponds to 5489
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
118
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
119 elseif (isstruct (arg1) && isscalar (arg1) && nargin == 1)
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
120 if (numfields (arg1) != 3
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
121 || ! all (isfield (arg1, {"Type", "Seed", "State"})))
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
122 error ('rng: input structure requires "Type", "Seed", "State" fields"');
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
123 endif
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
124 ## Only the internal state "State" and generator type "Type" are needed
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
125 generator = arg1.Type;
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
126 if (iscell (arg1.State))
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
127 [s_rand, s_randn] = deal (arg1.State{:});
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
128 else
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
129 s_rand = s_randn = arg1.State;
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
130 endif
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
131
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
132 else
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
133 print_usage ();
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
134 endif
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
135
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
136 ## Set the type of random number generator and seed it
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
137 if (isempty (generator))
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
138 generator = srng.Type;
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
139 endif
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
140 switch (generator)
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
141 case "twister"
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
142 rand ("state", s_rand);
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
143 randn ("state", s_randn);
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
144
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
145 case "legacy"
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
146 rand ("seed", s_rand);
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
147 randn ("seed", s_randn);
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
148
28107
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
149 case "v5uniform"
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
150 rand ("seed", s_rand);
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
151
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
152 case "v5normal"
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
153 randn ("seed", s_randn);
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
154
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
155 otherwise
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
156 error ('rng: invalid GENERATOR: "%s"', generator);
28107
d320728d5d06 style fixes for rng; document and mention in NEWS file
John W. Eaton <jwe@octave.org>
parents: 28106
diff changeset
157
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
158 endswitch
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
159
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
160 if (nargout > 0)
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
161 s = srng;
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
162 endif
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
163
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
164 endfunction
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
165
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
166
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
167 function gen = check_generator (val)
28945
6e460773bdda maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents: 28693
diff changeset
168
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
169 if (isempty (val))
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
170 gen = "";
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
171 return;
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
172 elseif (! iscellstr (val))
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
173 error ("rng: GENERATOR must be a string");
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
174 endif
28945
6e460773bdda maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents: 28693
diff changeset
175
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
176 gen = tolower (char (val));
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
177 if (any (strcmp (gen, {"simdtwister", "combrecursive", "philox", "threefry", "multfibonacci", "v4"})))
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
178 error ('rng: random number generator "%s" is not available in Octave', gen);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
179 elseif (! any (strcmp (gen, {"twister", "v5uniform", "v5normal"})))
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
180 error ('rng: unknown random number generator "%s"', gen);
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
181 endif
28945
6e460773bdda maint: Use newlines after "function" and before "endfunction" for clarity.
Rik <rik@octave.org>
parents: 28693
diff changeset
182
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
183 endfunction
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
184
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
185
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
186 %!test
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
187 %! state = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
188 %! unwind_protect
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
189 %! rng (42);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
190 %! ru1 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
191 %! rn1 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
192 %! rng (42);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
193 %! ru2 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
194 %! rn2 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
195 %! assert (ru2, ru1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
196 %! assert (rn2, rn1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
197 %! s1 = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
198 %! s2 = rng (42);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
199 %! assert (isequal (s1, s2));
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
200 %! ru1 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
201 %! rn1 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
202 %! s3 = rng (42);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
203 %! ru2 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
204 %! rn2 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
205 %! assert (ru2, ru1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
206 %! assert (rn2, rn1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
207 %! unwind_protect_cleanup
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
208 %! rng (state);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
209 %! end_unwind_protect
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
210
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
211 %!test
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
212 %! state = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
213 %! unwind_protect
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
214 %! rng (42, "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
215 %! ru1 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
216 %! rn1 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
217 %! rng (42, "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
218 %! ru2 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
219 %! rn2 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
220 %! assert (ru2, ru1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
221 %! assert (rn2, rn1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
222 %! s1 = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
223 %! s2 = rng (42, "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
224 %! assert (isequal (s1, s2));
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
225 %! ru1 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
226 %! rn1 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
227 %! s3 = rng (42, "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
228 %! ru2 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
229 %! rn2 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
230 %! assert (ru2, ru1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
231 %! assert (rn2, rn1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
232 %! unwind_protect_cleanup
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
233 %! rng (state);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
234 %! end_unwind_protect
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
235
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
236 %!test
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
237 %! state = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
238 %! unwind_protect
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
239 %! rng ("shuffle");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
240 %! rng ("shuffle", "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
241 %! s = rng ("shuffle");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
242 %! assert (! isequal (s, rng ("shuffle")));
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
243 %! s = rng ("shuffle", "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
244 %! assert (! isequal (s, rng ("shuffle", "twister")));
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
245 %! unwind_protect_cleanup
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
246 %! rng (state);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
247 %! end_unwind_protect
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
248
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
249 %!test
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
250 %! state = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
251 %! unwind_protect
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
252 %! rng ("default");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
253 %! ru1 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
254 %! rn1 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
255 %! rng (0, "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
256 %! ru2 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
257 %! rn2 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
258 %! assert (ru2, ru1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
259 %! assert (rn2, rn1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
260 %! rng (0, "twister");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
261 %! s = rng ("default");
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
262 %! assert (isequal (s, rng ()));
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
263 %! unwind_protect_cleanup
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
264 %! rng (state);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
265 %! end_unwind_protect
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
266
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
267 %!test
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
268 %! state = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
269 %! unwind_protect
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
270 %! s = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
271 %! ru1 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
272 %! rn1 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
273 %! rng (s);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
274 %! ru2 = rand ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
275 %! rn2 = randn ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
276 %! assert (ru2, ru1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
277 %! assert (rn2, rn1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
278 %! rng (42); rand (1,2); x = rand (1,2);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
279 %! rng (42); rand (1,2); s = rng (); y = rand (1,2);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
280 %! assert (x, y);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
281 %! rng (s); z = rand (1,2);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
282 %! assert (x, z);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
283 %! s1 = rng ();
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
284 %! s2 = rng (s1);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
285 %! assert (isequal (s1, s2));
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
286 %! unwind_protect_cleanup
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
287 %! rng (state);
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
288 %! end_unwind_protect
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
289
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
290 ## Test input validation
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
291 %!error <Invalid call> rng (1, 2, 3)
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
292 %!error <Invalid call> rng (eye (2))
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
293 %!error <Invalid call> rng ({})
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
294 %!error <Invalid call> rng (2i)
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
295 %!error <Invalid call> rng (-2)
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
296 %!error <Invalid call> rng ("foobar")
28106
c08d7b53f1a2 new function, rng
Guillaume Flandin
parents:
diff changeset
297 %!error <Invalid call> rng ("default", "twister")
28111
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
298 %!error <Invalid call> rng (struct ("Seed", {1, 2}))
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
299 %!error <Invalid call> rng (struct ("Type",[],"State",[],"Seed",[]), "twister")
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
300 %!error <input structure requires "Type"> rng (struct ())
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
301 %!error <input structure requires "Type">
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
302 %! rng (struct ("Type1",[],"State",[],"Seed",[]));
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
303 %!error <GENERATOR must be a string> rng (0, struct ())
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
304 %!error <"philox" is not available in Octave> rng (0, "philox")
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
305 %!error <GENERATOR must be a string> rng ("shuffle", struct ())
7fa1d6f670f5 rng.m: More style fixes.
Rik <rik@octave.org>
parents: 28107
diff changeset
306 %!error <unknown random number generator "foobar"> rng ("shuffle", "foobar")