1 ## Copyright (C) 2010-2012 Rik Wehbring
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} randi (@var{imax})
21 ## @deftypefnx {Function File} {} randi (@var{imax}, @var{n})
22 ## @deftypefnx {Function File} {} randi (@var{imax}, @var{m}, @var{n}, @dots{})
23 ## @deftypefnx {Function File} {} randi ([@var{imin} @var{imax}], @dots{})
24 ## @deftypefnx {Function File} {} randi (@dots{}, "@var{class}")
25 ## Return random integers in the range 1:@var{imax}.
27 ## Additional arguments determine the shape of the return matrix. When no
28 ## arguments are specified a single random integer is returned. If one
29 ## argument @var{n} is specified then a square matrix @w{(@var{n} x @var{n})} is
30 ## returned. Two or more arguments will return a multi-dimensional
31 ## matrix @w{(@var{m} x @var{n} x @dots{})}.
33 ## The integer range may optionally be described by a two element matrix
34 ## with a lower and upper bound in which case the returned integers will be
35 ## on the interval @w{[@var{imin}, @var{imax}]}.
37 ## The optional argument "@var{class}" will return a matrix of the requested
38 ## type. The default is "double".
40 ## The following example returns 150 integers in the range 1-10.
43 ## ri = randi (10, 150, 1)
46 ## Implementation Note: @code{randi} relies internally on @code{rand} which
47 ## uses class "double" to represent numbers. This limits the maximum
48 ## integer (@var{imax}) and range (@var{imax} - @var{imin}) to the value
49 ## returned by the @code{bitmax} function. For IEEE floating point numbers
50 ## this value is @w{@math{2^{53} - 1}}.
55 ## Author: Rik Wehbring
57 function ri = randi (bounds, varargin)
63 if (! (isnumeric (bounds) && isreal (bounds)))
64 error ("randi: IMIN and IMAX must be real numeric bounds");
67 if (isscalar (bounds))
71 error ("randi: require IMAX >= 1");
74 imin = fix (bounds(1));
75 imax = fix (bounds(2));
77 error ("randi: require IMIN <= IMAX");
81 if (nargin > 1 && ischar (varargin{end}))
82 rclass = varargin{end};
88 if (strfind (rclass, "int"))
89 if (imax > intmax (rclass))
90 error ("randi: require IMAX < intmax (CLASS)");
92 elseif (strcmp (rclass, "single"))
93 if (imax > bitmax (rclass))
94 error ("randi: require IMAX < bitmax (CLASS)");
97 ## Limit set by use of class double in rand()
99 error ("randi: maximum integer IMAX must be smaller than bitmax ()");
101 if ((imax - imin) > bitmax)
102 error ("randi: maximum integer range must be smaller than bitmax ()");
106 ri = imin + floor ( (imax-imin+1)*rand (varargin{:}) );
108 if (! strcmp (rclass, "double"))
109 ri = cast (ri, rclass);
115 %! ri = randi (10, 1000, 1);
116 %! assert(isequal(ri, fix (ri)));
117 %! assert(min(ri) == 1);
118 %! assert(max(ri) == 10);
119 %! assert(rows(ri) == 1000);
120 %! assert(columns(ri) == 1);
121 %! assert(strcmp (class (ri), "double"));
123 %! ri = randi ([-5, 10], 1000, 1, "int8");
124 %! assert(isequal(ri, fix (ri)));
125 %! assert(min(ri) == -5);
126 %! assert(max(ri) == 10);
127 %! assert(strcmp (class (ri), "int8"));
129 %!assert(size (randi(10, 3,1,2)) == [3, 1, 2])
131 %% Test input validation
133 %!error(randi("test"))
134 %!error(randi(10+2i))
136 %!error(randi([10, 1]))
137 %!error(randi(256, "uint8"))
138 %!error(randi(2^25, "single"))
139 %!error(randi(bitmax() + 1))
140 %!error(randi([-1, bitmax()]))