Mercurial > octave
annotate scripts/general/randi.m @ 22760:c4d80b9d2898
maint: Capitalize variable names appearing in error() messages of m-files.
* scripts/audio/@audioplayer/get.m, scripts/audio/@audiorecorder/get.m,
interp1.m, narginchk.m, nargoutchk.m, randi.m, __fltk_file_filter__.m,
imshow.m, rgbplot.m, javachk.m, usejava.m, condest.m, unpack.m, pqpnonneg.m,
savepath.m, default_prefix.m, get_forge_download.m, get_forge_pkg.m,
installed_packages.m, annotation.m, daspect.m, pbaspect.m, isocaps.m, pan.m,
printd.m, __gnuplot_draw_axes__.m, rotate.m, rotate3d.m, zoom.m, splinefit.m,
addpref.m, getpref.m, rmpref.m, eigs.m, normrnd.m, t_test_regression.m:
Capitalize variable names appearing in error() messages.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 14 Nov 2016 12:12:02 -0800 |
parents | 3a2b891d0b33 |
children | 01aae08a0105 |
rev | line source |
---|---|
22323
bac0d6f07a3e
maint: Update copyright notices for 2016.
John W. Eaton <jwe@octave.org>
parents:
20852
diff
changeset
|
1 ## Copyright (C) 2010-2016 Rik Wehbring |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
2 ## |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
3 ## This file is part of Octave. |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
4 ## |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22627
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22627
diff
changeset
|
8 ## (at your option) any later version. |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
9 ## |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
10 ## 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
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22627
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22627
diff
changeset
|
13 ## GNU General Public License for more details. |
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 ## 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
|
16 ## along with Octave; see the file COPYING. If not, see |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
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 ## -*- texinfo -*- |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20773
diff
changeset
|
20 ## @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
|
21 ## @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
|
22 ## @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
|
23 ## @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
|
24 ## @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
|
25 ## 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
|
26 ## |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
27 ## 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
|
28 ## 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
|
29 ## 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
|
30 ## 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
|
31 ## @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
|
32 ## |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## 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
|
34 ## 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
|
35 ## interval @w{[@var{imin}, @var{imax}]}. |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
36 ## |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
14846
diff
changeset
|
37 ## 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
|
38 ## type. The default is @qcode{"double"}. |
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 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
|
41 ## |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
42 ## @example |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## ri = randi (10, 150, 1) |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
44 ## @end example |
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 ## 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
|
47 ## 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
|
48 ## integer (@var{imax}) and range (@var{imax} - @var{imin}) to the value |
20767 | 49 ## returned by the @code{flintmax} function. For IEEE floating point numbers |
50 ## 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
|
51 ## |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
52 ## @seealso{rand} |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
53 ## @end deftypefn |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
54 |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
55 ## Author: Rik Wehbring |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
56 |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
57 function ri = randi (bounds, varargin) |
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 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
|
60 print_usage (); |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
61 endif |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
62 |
20766
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
63 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
|
64 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
|
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 bounds = real (double (bounds)); |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
68 if (isscalar (bounds)) |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
69 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
|
70 imax = bounds; |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
71 if (imax < 1) |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
72 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
|
73 endif |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
74 else |
20766
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
75 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
|
76 imax = bounds(2); |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
77 if (imax < imin) |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
78 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
|
79 endif |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
80 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
81 |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
82 if (nargin > 1 && ischar (varargin{end})) |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
83 rclass = varargin{end}; |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
84 varargin(end) = []; |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
85 else |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
86 rclass = "double"; |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
87 endif |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
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 | 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 |
20766
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
98 ri = imin + floor ((imax - imin + 1) * rand (varargin{:})); |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
99 |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 minval = -maxval; |
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
107 else |
22760
c4d80b9d2898
maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents:
22755
diff
changeset
|
108 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
|
109 endif |
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
110 if (imax > maxval) |
20767 | 111 warning (["randi: integer IMAX exceeds requested type. ", ... |
112 "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
|
113 elseif (imin < minval) |
20767 | 114 warning (["randi: integer IMIN exceeds requested type. ", ... |
115 " 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
|
116 endif |
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
117 |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
118 ri = cast (ri, rclass); |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
119 endif |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
120 |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
121 endfunction |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
122 |
17338
1c89599167a6
maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents:
17281
diff
changeset
|
123 |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
124 %!test |
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
125 %! 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
|
126 %! assert (ri, fix (ri)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
127 %! assert (min (ri), 1); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
128 %! assert (max (ri), 10); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
129 %! assert (rows (ri), 1000); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
130 %! assert (columns (ri), 1); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
131 %! assert (class (ri), "double"); |
20767 | 132 ## FIXME: Does Octave guarantee support for int64 even when underlying hardware |
133 ## is 32-bit? | |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
134 %!test |
20766
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
135 %! 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
|
136 %! 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
|
137 %! 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
|
138 %! 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
|
139 %! 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
|
140 %! 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
|
141 %! 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
|
142 %!test |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
143 %! 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
|
144 %! assert (ri, fix (ri)); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
145 %! 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
|
146 %! 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
|
147 %! 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
|
148 %!test |
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
149 %! 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
|
150 %! 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
|
151 %! 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
|
152 %! 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
|
153 %! assert (class (ri), "single"); |
20767 | 154 |
20766
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
155 %!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
|
156 |
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
157 %!shared max_int8, min_int8, max_uint8, min_uint8, max_single |
20767 | 158 %! max_int8 = double (intmax ("int8")); |
159 %! min_int8 = double (intmin ("int8")); | |
160 %! max_uint8 = double (intmax ("uint8")); | |
161 %! min_uint8 = double (intmin ("uint8")); | |
162 %! 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
|
163 |
20767 | 164 ## 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
|
165 %!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
|
166 %! state = warning ("query"); |
7b190a2f11cb
maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
167 %! unwind_protect |
7b190a2f11cb
maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
168 %! warning ("error", "all"); |
7b190a2f11cb
maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
169 %! func (varargin{:}); |
7b190a2f11cb
maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
170 %! unwind_protect_cleanup |
7b190a2f11cb
maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
171 %! warning (state); |
7b190a2f11cb
maint: Use 2-space indent in definition of BIST %!functions.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
172 %! 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
|
173 %!endfunction |
a34e5739538c
randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents:
20158
diff
changeset
|
174 %!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
|
175 %!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
|
176 %!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
|
177 %!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
|
178 %!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
|
179 %!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
|
180 |
20767 | 181 ## 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
|
182 %!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
|
183 %! 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
|
184 %!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
|
185 %! 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
|
186 %!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
|
187 %! 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
|
188 %!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
|
189 %! 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
|
190 %!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
|
191 %! 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
|
192 %!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
|
193 %! 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
|
194 %! 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
|
195 %! 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
|
196 %! 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
|
197 %! 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
|
198 |
10892
20ce631f0f12
Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff
changeset
|
199 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
200 ## Test input validation |
20767 | 201 %!error randi () |
202 %!error <must be integer bounds> randi ("test") | |
203 %!error <must be integer bounds> randi (struct ("a", 1)) | |
204 %!error <must be integer bounds> randi (1.5) | |
205 %!error <must be integer bounds> randi ([1.5, 2.5]) | |
206 %!error <must be integer bounds> randi ([1, 2.5]) | |
207 %!error <must be integer bounds> randi ([1.5, 2]) | |
208 %!error <require IMAX .= 1> randi (0) | |
209 %!error <require IMIN <= IMAX> randi ([10, 1]) | |
210 %!error <IMIN and IMAX must be smaller than flintmax\(\)> randi (flintmax ()) | |
211 %!error <range must be smaller than flintmax\(\)-1> randi ([-1, flintmax() - 1]) | |
212 %!error <unknown requested output class 'foo'> randi (10, "foo") | |
213 |