annotate scripts/general/randi.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 2010-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26617
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26617
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26617
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26617
diff changeset
5 ##
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
6 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
7 ## This file is part of Octave.
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
10 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22627
diff changeset
12 ## (at your option) any later version.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
13 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22627
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22627
diff changeset
17 ## GNU General Public License for more details.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
18 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
19 ## You should have received a copy of the GNU General Public License
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
20 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
21 ## <https://www.gnu.org/licenses/>.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
22
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
23 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20773
diff changeset
24 ## @deftypefn {} {} randi (@var{imax})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20773
diff changeset
25 ## @deftypefnx {} {} randi (@var{imax}, @var{n})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20773
diff changeset
26 ## @deftypefnx {} {} randi (@var{imax}, @var{m}, @var{n}, @dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20773
diff changeset
27 ## @deftypefnx {} {} randi ([@var{imin} @var{imax}], @dots{})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20773
diff changeset
28 ## @deftypefnx {} {} randi (@dots{}, "@var{class}")
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
29 ## Return random integers in the range 1:@var{imax}.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
30 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
31 ## Additional arguments determine the shape of the return matrix. When no
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
32 ## arguments are specified a single random integer is returned. If one
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## argument @var{n} is specified then a square matrix @w{(@var{n} x @var{n})}
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
34 ## is returned. Two or more arguments will return a multi-dimensional matrix
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
35 ## @w{(@var{m} x @var{n} x @dots{})}.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
36 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
37 ## The integer range may optionally be described by a two element matrix with a
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
38 ## lower and upper bound in which case the returned integers will be on the
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
39 ## interval @w{[@var{imin}, @var{imax}]}.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
40 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14846
diff changeset
41 ## The optional argument @var{class} will return a matrix of the requested
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14846
diff changeset
42 ## type. The default is @qcode{"double"}.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
43 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
44 ## The following example returns 150 integers in the range 1--10.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
45 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
46 ## @example
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
47 ## ri = randi (10, 150, 1)
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
48 ## @end example
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
49 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
50 ## Implementation Note: @code{randi} relies internally on @code{rand} which
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14846
diff changeset
51 ## uses class @qcode{"double"} to represent numbers. This limits the maximum
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
52 ## integer (@var{imax}) and range (@var{imax} - @var{imin}) to the value
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
53 ## returned by the @code{flintmax} function. For IEEE floating point numbers
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
54 ## this value is @w{@math{2^{53} - 1}}.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
55 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
56 ## @seealso{rand}
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
57 ## @end deftypefn
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
58
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
59 ## Author: Rik Wehbring
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
60
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
61 function ri = randi (bounds, varargin)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
62
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
63 if (nargin < 1)
14846
460a3c6d8bf1 maint: Use Octave coding convention for cuddled parenthis in function calls with empty argument lists.
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
64 print_usage ();
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
65 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
66
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
67 if (! (isnumeric (bounds) && all (bounds == fix (bounds))))
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
68 error ("randi: IMIN and IMAX must be integer bounds");
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
69 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
70
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
71 bounds = real (double (bounds));
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
72 if (isscalar (bounds))
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
73 imin = 1;
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
74 imax = bounds;
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
75 if (imax < 1)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
76 error ("randi: require IMAX >= 1");
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
77 endif
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
78 else
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
79 imin = bounds(1);
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
80 imax = bounds(2);
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
81 if (imax < imin)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
82 error ("randi: require IMIN <= IMAX");
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
83 endif
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
84 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
85
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
86 ## Limit set by use of class double in rand(): Any consecutive integer in the
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
87 ## range [-flintmax(), flintmax()] can be represented by a double.
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
88 if ((abs (imax) >= flintmax ()) || (abs (imin) >= flintmax ()))
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
89 error ("randi: IMIN and IMAX must be smaller than flintmax()");
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
90 endif
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
91 if ((imax - imin) >= (flintmax () - 1))
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
92 error ("randi: integer range must be smaller than flintmax()-1");
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
93 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
94
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
95 if (nargin > 1 && ischar (varargin{end}))
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
96 rclass = varargin{end};
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
97 varargin(end) = [];
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
98 nargin = nargin - 1;
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
99 else
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
100 rclass = "double";
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
101 endif
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
102
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
103 ## Expand dimension argument to at least 2-D for reshape
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
104 if (nargin == 1)
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
105 varargin = {1, 1};
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
106 elseif (nargin == 2 && isscalar (varargin{1}))
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
107 varargin(2) = varargin(1);
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
108 endif
26617
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
109
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
110 ## Rejection Algorithm to guarantee unbiased results. See bug #54619.
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
111 rng = (imax - imin) + 1; # requested range
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
112 N = prod ([varargin{:}]); # number of requested elements
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
113 K = floor ((flintmax () + 1) / rng); # number of primary integers ...
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
114 # mapped to single output
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
115 p = (K*rng) / (flintmax () + 1); # expected proportion of used primaries
26617
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
116
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
117 do
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
118 M = ceil (N/p + 10*sqrt (N/p - N)); # number of requested primary integers
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
119 r_prim = floor (rand (M,1) * (flintmax () + 1));
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
120 r_prim = r_prim(r_prim < K*rng);
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
121 until (numel (r_prim) >= N) # should practically always be true
26617
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
122
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
123 ri = imin + floor (reshape (r_prim(1:N), varargin{:}) / K);
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
124
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
125 if (! strcmp (rclass, "double"))
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
126 if (strfind (rclass, "int"))
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
127 maxval = double (intmax (rclass));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
128 minval = double (intmin (rclass));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
129 elseif (strcmp (rclass, "single"))
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
130 maxval = double (flintmax (rclass));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
131 minval = -maxval;
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
132 else
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
133 error ("randi: unknown requested output CLASS '%s'", rclass);
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
134 endif
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
135 if (imax > maxval)
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
136 warning (["randi: integer IMAX exceeds requested type. ", ...
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
137 "Values might be truncated to requested type."]);
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
138 elseif (imin < minval)
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
139 warning (["randi: integer IMIN exceeds requested type. ", ...
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
140 " Values might be truncated to requested type."]);
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
141 endif
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
142
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
143 ri = cast (ri, rclass);
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
144 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
145
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
146 endfunction
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
147
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
148
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
149 %!test
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
150 %! ri = randi (10, 1000, 1);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
151 %! assert (ri, fix (ri));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
152 %! assert (min (ri), 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
153 %! assert (max (ri), 10);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
154 %! assert (rows (ri), 1000);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
155 %! assert (columns (ri), 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
156 %! assert (class (ri), "double");
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
157 ## FIXME: Does Octave guarantee support for int64 even when underlying hardware
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
158 ## is 32-bit?
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
159 %!test
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
160 %! ri = randi (int64 (100), 1, 1000);
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
161 %! assert (ri, fix (ri));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
162 %! assert (min (ri), 1);
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
163 %! assert (max (ri), 100);
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
164 %! assert (rows (ri), 1);
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
165 %! assert (columns (ri), 1000);
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
166 %! assert (class (ri), "double");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
167 %!test
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
168 %! ri = randi ([-5, 10], 1000, 1, "int8");
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
169 %! assert (ri, fix (ri));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
170 %! assert (min (ri), int8 (-5));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
171 %! assert (max (ri), int8 (10));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
172 %! assert (class (ri), "int8");
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
173 %!test
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
174 %! ri = randi ([-5; 10], 1000, 1, "single");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
175 %! assert (ri, fix (ri));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
176 %! assert (min (ri), single (-5));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
177 %! assert (max (ri), single (10));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
178 %! assert (class (ri), "single");
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
179
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
180 %!assert (size (randi (10, 3, 1, 2)), [3, 1, 2])
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
181
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
182 %!shared max_int8, min_int8, max_uint8, min_uint8, max_single
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
183 %! max_int8 = double (intmax ("int8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
184 %! min_int8 = double (intmin ("int8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
185 %! max_uint8 = double (intmax ("uint8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
186 %! min_uint8 = double (intmin ("uint8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
187 %! max_single = double (flintmax ("single"));
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
188
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
189 ## Test that no warning thrown if IMAX is exactly on the limits of the range
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
190 %!function test_no_warning (func, varargin)
22627
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
191 %! state = warning ("query");
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
192 %! unwind_protect
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
193 %! warning ("error", "all");
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
194 %! func (varargin{:});
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
195 %! unwind_protect_cleanup
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
196 %! warning (state);
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
197 %! end_unwind_protect
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
198 %!endfunction
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
199 %!test test_no_warning (@randi, max_int8, "int8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
200 %!test test_no_warning (@randi, max_uint8, "uint8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
201 %!test test_no_warning (@randi, max_single, "single");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
202 %!test test_no_warning (@randi, [min_int8, max_int8], "int8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
203 %!test test_no_warning (@randi, [min_uint8, max_uint8], "uint8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
204 %!test test_no_warning (@randi, [-max_single, max_single], "single");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
205
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
206 ## Test exceeding range
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
207 %!warning <exceeds requested type>
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
208 %! randi ([min_int8-1, max_int8], "int8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
209 %!warning <exceeds requested type>
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
210 %! randi ([min_uint8-1, max_uint8], "uint8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
211 %!warning <exceeds requested type>
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
212 %! randi ([min_int8, max_int8 + 1], "int8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
213 %!warning <exceeds requested type>
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
214 %! randi ([min_uint8, max_uint8 + 1], "uint8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
215 %!warning <exceeds requested type>
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
216 %! randi ([0, max_single + 1], "single");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
217 %!warning <exceeds requested type>
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
218 %! ri = randi ([-5, 10], 1000, 1, "uint8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
219 %! assert (ri, fix (ri));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
220 %! assert (min (ri), uint8 (-5));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
221 %! assert (max (ri), uint8 (10));
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
222 %! assert (class (ri), "uint8");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
223
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
224
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
225 ## Test input validation
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
226 %!error randi ()
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
227 %!error <must be integer bounds> randi ("test")
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
228 %!error <must be integer bounds> randi (struct ("a", 1))
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
229 %!error <must be integer bounds> randi (1.5)
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
230 %!error <must be integer bounds> randi ([1.5, 2.5])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
231 %!error <must be integer bounds> randi ([1, 2.5])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
232 %!error <must be integer bounds> randi ([1.5, 2])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
233 %!error <require IMAX .= 1> randi (0)
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
234 %!error <require IMIN <= IMAX> randi ([10, 1])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
235 %!error <IMIN and IMAX must be smaller than flintmax\(\)> randi (flintmax ())
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
236 %!error <range must be smaller than flintmax\(\)-1> randi ([-1, flintmax() - 1])
22765
01aae08a0105 maint: Rename variables to match documentation in m-files.
Rik <rik@octave.org>
parents: 22760
diff changeset
237 %!error <unknown requested output CLASS 'foo'> randi (10, "foo")