annotate scripts/general/randi.m @ 27923:bd51beb6205e

update formatting of copyright notices * Use <https://octave.org/copyright/> instead of <https://octave.org/COPYRIGHT.html/>. * For consistency with other comments in the Octave sources, use C++-style comments for copyright blocks in C and C++ files. * Use delimiters above and below copyright blocks that are appropriate for the language used in the file. * Eliminate extra spacing inside copyright blocks. * lex.ll (looks_like_copyright): Also allow newlines and carriage returns before the word "Copyright". * scripts/mk-doc.pl (gethelp): Also skip empty comment lines. * bp-table.cc, type.m: Adjust tests.
author John W. Eaton <jwe@octave.org>
date Wed, 08 Jan 2020 11:59:41 -0500
parents 1891570abac8
children a4268efb7334
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 ##
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
3 ## 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
4 ##
27923
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/>.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
7 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
8 ## This file is part of Octave.
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## 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
11 ## 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
12 ## 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
13 ## (at your option) any later version.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
14 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
15 ## 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
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22627
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22627
diff changeset
18 ## GNU General Public License for more details.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
19 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
20 ## 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
21 ## 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
22 ## <https://www.gnu.org/licenses/>.
27923
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 ########################################################################
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
25
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20773
diff changeset
27 ## @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
28 ## @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
29 ## @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
30 ## @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
31 ## @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
32 ## 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
33 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
34 ## 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
35 ## 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
36 ## 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
37 ## 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
38 ## @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
39 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
40 ## 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
41 ## 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
42 ## interval @w{[@var{imin}, @var{imax}]}.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
43 ##
17281
bc924baa2c4e doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents: 14846
diff changeset
44 ## 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
45 ## type. The default is @qcode{"double"}.
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
46 ##
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
47 ## 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
48 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
49 ## @example
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
50 ## ri = randi (10, 150, 1)
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
51 ## @end example
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
52 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
53 ## 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
54 ## 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
55 ## 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
56 ## 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
57 ## 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
58 ##
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
59 ## @seealso{rand}
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
60 ## @end deftypefn
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
61
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
62 ## Author: Rik Wehbring
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
63
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
64 function ri = randi (bounds, varargin)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
65
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
66 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
67 print_usage ();
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
68 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
69
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
70 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
71 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
72 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
73
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
74 bounds = real (double (bounds));
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
75 if (isscalar (bounds))
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
76 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
77 imax = bounds;
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
78 if (imax < 1)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
79 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
80 endif
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
81 else
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
82 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
83 imax = bounds(2);
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
84 if (imax < imin)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
85 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
86 endif
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
87 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
88
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
89 ## 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
90 ## range [-flintmax(), flintmax()] can be represented by a double.
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
91 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
92 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
93 endif
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
94 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
95 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
96 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
97
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
98 if (nargin > 1 && ischar (varargin{end}))
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
99 rclass = varargin{end};
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
100 varargin(end) = [];
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
101 nargin = nargin - 1;
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
102 else
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
103 rclass = "double";
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
104 endif
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
105
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
106 ## 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
107 if (nargin == 1)
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
108 varargin = {1, 1};
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
109 elseif (nargin == 2 && isscalar (varargin{1}))
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
110 varargin(2) = varargin(1);
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
111 endif
26617
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
112
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
113 ## 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
114 rng = (imax - imin) + 1; # requested range
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
115 N = prod ([varargin{:}]); # number of requested elements
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
116 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
117 # mapped to single output
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
118 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
119
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
120 do
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
121 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
122 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
123 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
124 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
125
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
126 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
127
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
128 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
129 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
130 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
131 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
132 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
133 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
134 minval = -maxval;
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
135 else
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
136 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
137 endif
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
138 if (imax > maxval)
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
139 warning (["randi: integer IMAX 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 elseif (imin < minval)
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
142 warning (["randi: integer IMIN exceeds requested type. ", ...
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
143 " 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
144 endif
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
145
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
146 ri = cast (ri, rclass);
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
147 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
148
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
149 endfunction
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
150
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
151
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
152 %!test
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
153 %! 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
154 %! assert (ri, fix (ri));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
155 %! assert (min (ri), 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
156 %! assert (max (ri), 10);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
157 %! assert (rows (ri), 1000);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
158 %! assert (columns (ri), 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
159 %! assert (class (ri), "double");
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
160 ## 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
161 ## is 32-bit?
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
162 %!test
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
163 %! 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
164 %! 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
165 %! 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
166 %! 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
167 %! 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
168 %! 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
169 %! 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
170 %!test
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
171 %! 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
172 %! assert (ri, fix (ri));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
173 %! 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
174 %! 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
175 %! 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
176 %!test
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
177 %! 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
178 %! 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
179 %! 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
180 %! 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
181 %! assert (class (ri), "single");
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
182
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
183 %!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
184
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
185 %!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
186 %! max_int8 = double (intmax ("int8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
187 %! min_int8 = double (intmin ("int8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
188 %! max_uint8 = double (intmax ("uint8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
189 %! min_uint8 = double (intmin ("uint8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
190 %! 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
191
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
192 ## 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
193 %!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
194 %! state = warning ("query");
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
195 %! unwind_protect
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
196 %! warning ("error", "all");
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
197 %! func (varargin{:});
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
198 %! unwind_protect_cleanup
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
199 %! warning (state);
7b190a2f11cb maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents: 22323
diff changeset
200 %! 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
201 %!endfunction
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, 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, 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, "single");
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
205 %!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
206 %!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
207 %!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
208
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
209 ## 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
210 %!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
211 %! 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
212 %!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
213 %! 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
214 %!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
215 %! 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
216 %!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
217 %! 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
218 %!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
219 %! 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
220 %!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
221 %! 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
222 %! 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
223 %! 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
224 %! 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
225 %! 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
226
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
227
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
228 ## Test input validation
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
229 %!error randi ()
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
230 %!error <must be integer bounds> randi ("test")
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
231 %!error <must be integer bounds> randi (struct ("a", 1))
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
232 %!error <must be integer bounds> randi (1.5)
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
233 %!error <must be integer bounds> randi ([1.5, 2.5])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
234 %!error <must be integer bounds> randi ([1, 2.5])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
235 %!error <must be integer bounds> randi ([1.5, 2])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
236 %!error <require IMAX .= 1> randi (0)
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
237 %!error <require IMIN <= IMAX> randi ([10, 1])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
238 %!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
239 %!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
240 %!error <unknown requested output CLASS 'foo'> randi (10, "foo")