annotate scripts/general/randi.m @ 30893:e1788b1a315f

maint: Use "fcn" as preferred abbreviation for "function" in m-files. * accumarray.m, accumdim.m, quadl.m, quadv.m, randi.m, structfun.m, __is_function__.m, uigetfile.m, uimenu.m, uiputfile.m, doc_cache_create.m, colorspace_conversion_input_check.m, imageIO.m, argnames.m, vectorize.m, vectorize.m, normest1.m, inputname.m, nthargout.m, display_info_file.m, decic.m, ode15i.m, ode15s.m, ode23.m, ode23s.m, ode45.m, odeset.m, check_default_input.m, integrate_adaptive.m, ode_event_handler.m, runge_kutta_23.m, runge_kutta_23s.m, runge_kutta_45_dorpri.m, runge_kutta_interpolate.m, starting_stepsize.m, __all_opts__.m, fminbnd.m, fminsearch.m, fminunc.m, fsolve.m, fzero.m, sqp.m, fplot.m, plotyy.m, __bar__.m, __ezplot__.m, flat_entry.html, profexport.m, movfun.m, bicg.m, bicgstab.m, cgs.m, eigs.m, gmres.m, pcg.m, __alltohandles__.m, __sprand__.m, qmr.m, tfqmr.m, dump_demos.m: Replace "func", "fun", "fn" in documentation and variable names with "fcn".
author Rik <rik@octave.org>
date Mon, 04 Apr 2022 18:14:56 -0700
parents 5d3faba0342e
children 597f3ee61a48
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 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2010-2022 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 -*-
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
27 ## @deftypefn {} {@var{R} =} randi (@var{imax})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
28 ## @deftypefnx {} {@var{R} =} randi (@var{imax}, @var{n})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
29 ## @deftypefnx {} {@var{R} =} randi (@var{imax}, @var{m}, @var{n}, @dots{})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
30 ## @deftypefnx {} {@var{R} =} randi ([@var{imin} @var{imax}], @dots{})
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
31 ## @deftypefnx {} {@var{R} =} 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 ##
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
40 ## The integer range may optionally be described by a two-element matrix with a
20158
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 ##
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
59 ## @seealso{rand, randn}
10892
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
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
62 function R = randi (bounds, varargin)
10892
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 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
65 print_usage ();
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
66 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
67
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
68 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
69 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
70 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
71
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
72 bounds = real (double (bounds));
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
73 if (isscalar (bounds))
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
74 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
75 imax = bounds;
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
76 if (imax < 1)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
77 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
78 endif
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
79 else
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
80 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
81 imax = bounds(2);
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
82 if (imax < imin)
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
83 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
84 endif
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
85 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
86
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
87 ## 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
88 ## range [-flintmax(), flintmax()] can be represented by a double.
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
89 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
90 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
91 endif
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
92 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
93 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
94 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
95
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
96 if (nargin > 1 && ischar (varargin{end}))
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
97 rclass = varargin{end};
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
98 varargin(end) = [];
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
99 nargin = nargin - 1;
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
100 else
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
101 rclass = "double";
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
102 endif
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
103
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
104 ## 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
105 if (nargin == 1)
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
106 varargin = {1, 1};
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
107 elseif (nargin == 2 && isscalar (varargin{1}))
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
108 varargin(2) = varargin(1);
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
109 endif
26617
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
110
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
111 ## 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
112 rng = (imax - imin) + 1; # requested range
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
113 N = prod ([varargin{:}]); # number of requested elements
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
114 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
115 # mapped to single output
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
116 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
117
98afb8bbd1f6 maint: Strip trailing whitespace from source files.
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
118 do
26264
25d3e8e49d5c randi.m: Implement rejection algorithm for unbiased results (bug #54619).
Michael Leitner
parents: 25054
diff changeset
119 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
120 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
121 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
122 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
123
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
124 R = 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
125
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
126 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
127 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
128 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
129 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
130 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
131 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
132 minval = -maxval;
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
133 else
22760
c4d80b9d2898 maint: Capitalize variable names appearing in error() messages of m-files.
Rik <rik@octave.org>
parents: 22755
diff changeset
134 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
135 endif
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
136 if (imax > maxval)
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
137 warning (["randi: integer IMAX exceeds requested type. ", ...
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
138 "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
139 elseif (imin < minval)
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
140 warning (["randi: integer IMIN exceeds requested type. ", ...
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
141 " 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
142 endif
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
143
30875
5d3faba0342e doc: Ensure documentation lists output argument when it exists for all m-files.
Rik <rik@octave.org>
parents: 30564
diff changeset
144 R = cast (R, rclass);
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
145 endif
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
146
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
147 endfunction
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
148
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 17281
diff changeset
149
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
150 %!test
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
151 %! 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
152 %! assert (ri, fix (ri));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
153 %! assert (min (ri), 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
154 %! assert (max (ri), 10);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
155 %! assert (rows (ri), 1000);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
156 %! assert (columns (ri), 1);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
157 %! assert (class (ri), "double");
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
158 ## 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
159 ## is 32-bit?
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
160 %!test
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
161 %! 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
162 %! 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
163 %! 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
164 %! 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
165 %! 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
166 %! 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
167 %! 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
168 %!test
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
169 %! 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
170 %! assert (ri, fix (ri));
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
171 %! 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
172 %! 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
173 %! 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
174 %!test
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
175 %! 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
176 %! 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
177 %! 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
178 %! 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
179 %! assert (class (ri), "single");
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
180
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
181 %!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
182
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
183 %!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
184 %! max_int8 = double (intmax ("int8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
185 %! min_int8 = double (intmin ("int8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
186 %! max_uint8 = double (intmax ("uint8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
187 %! min_uint8 = double (intmin ("uint8"));
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
188 %! 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
189
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
190 ## Test that no warning thrown if IMAX is exactly on the limits of the range
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30875
diff changeset
191 %!function test_no_warning (fcn, varargin)
28271
5e1f4060289a Make 1-input form "warning (STATE)" use special ID "all" (bug #58296).
Rik <rik@octave.org>
parents: 27978
diff changeset
192 %! lastwarn ("");
30893
e1788b1a315f maint: Use "fcn" as preferred abbreviation for "function" in m-files.
Rik <rik@octave.org>
parents: 30875
diff changeset
193 %! fcn (varargin{:});
28271
5e1f4060289a Make 1-input form "warning (STATE)" use special ID "all" (bug #58296).
Rik <rik@octave.org>
parents: 27978
diff changeset
194 %! assert (lastwarn (), "");
20766
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
195 %!endfunction
a34e5739538c randi.m: corrected warning policy for int and single ranges.
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 20158
diff changeset
196 %!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
197 %!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
198 %!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
199 %!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
200 %!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
201 %!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
202
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
203 ## 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
204 %!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
205 %! 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
206 %!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
207 %! 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
208 %!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
209 %! 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
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_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
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 ([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
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 %! 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
216 %! 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
217 %! 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
218 %! 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
219 %! 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
220
10892
20ce631f0f12 Add new randi function for random integers.
Rik <octave@nomad.inbox5.com>
parents:
diff changeset
221
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
222 ## Test input validation
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 28271
diff changeset
223 %!error <Invalid call> randi ()
20767
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
224 %!error <must be integer bounds> randi ("test")
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
225 %!error <must be integer bounds> randi (struct ("a", 1))
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
226 %!error <must be integer bounds> randi (1.5)
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
227 %!error <must be integer bounds> randi ([1.5, 2.5])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
228 %!error <must be integer bounds> randi ([1, 2.5])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
229 %!error <must be integer bounds> randi ([1.5, 2])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
230 %!error <require IMAX .= 1> randi (0)
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
231 %!error <require IMIN <= IMAX> randi ([10, 1])
e0e55ce86ba0 randi.m: Clean up function.
Rik <rik@octave.org>
parents: 20766
diff changeset
232 %!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
233 %!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
234 %!error <unknown requested output CLASS 'foo'> randi (10, "foo")