]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/randint.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / randint.m
1 ## Copyright (C) 2001 Laurent Mazet
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{b} = } randint (@var{n})
18 ## @deftypefnx {Function File} {@var{b} = } randint (@var{n},@var{m})
19 ## @deftypefnx {Function File} {@var{b} = } randint (@var{n},@var{m},@var{range})
20 ## @deftypefnx {Function File} {@var{b} = } randint (@var{n},@var{m},@var{range},@var{seed})
21 ##
22 ## Generate a matrix of random binary numbers. The size of the matrix is
23 ## @var{n} rows by @var{m} columns. By default @var{m} is equal to @var{n}.
24 ##
25 ## The range in which the integers are generated will is determined by
26 ## the variable @var{range}. If @var{range} is an integer, the value will
27 ## lie in the range [0,@var{range}-1], or [@var{range}+1,0] if @var{range}
28 ## is negative. If @var{range} contains two elements the intgers will lie 
29 ## within these two elements, inclusive. By default @var{range} is 
30 ## assumed to be [0:1].
31 ##
32 ## The variable @var{seed} allows the random number generator to be seeded
33 ## with a fixed value. The initial seed will be restored when returning.
34 ## @end deftypefn
35
36 ## 2001 FEB 07
37 ##   initial release
38
39 function b = randint (n, m, range, seed)
40
41   switch (nargin)
42     case 1,
43       m = n;
44       range = [0,1];
45       seed = Inf;
46     case 2,
47       range = [0,1];
48       seed = Inf;
49     case 3,
50       seed = Inf;      
51     case 4,
52     otherwise
53       usage ("b = randint (n, [m, [range, [seed]]])");
54   endswitch
55
56   ## Check range
57   if (length (range) == 1)
58     if (range < 0)
59       range = [range+1, 0];
60     else
61       range = [0, range-1];
62     endif
63   elseif ( prod (size (range)) != 2)
64     error ("randint: range must be a 2 element vector");
65   endif
66   range = sort (range);
67   
68   ## Check seed;
69   if (!isinf (seed))
70     old_seed = rand ("seed");
71     rand ("seed", seed);
72   endif
73
74   b = range (1) - 1 + ceil (rand (n, m) * (range (2) - range (1) + 1));
75   
76   ## Get back to the old
77   if (!isinf (seed))
78     rand ("seed", old_seed);
79   endif
80
81 endfunction
82
83 %!shared n, m, seed, a1, a2, a3, a4, a5, a6
84 %!    n = 10; m = 32; seed = 1; a1 = randint(n); a2 = randint(n,n); 
85 %!    a3 = randint(n,n,m); a4 = randint(n,n,[-m,m]); 
86 %!    a5 = randint(n,n,m, seed); a6 = randint(n,n,m, seed);
87
88 %!error randint ();
89 %!error randint (n,n,n,n,n);
90 %!assert (size(a1) == [n, n] && size(a2) == [n, n]);
91 %!assert (max ([a1(:); a2(:)]) <= 1 && min([a1(:); a2(:)]) >= 0);
92 %!assert (size(a3) == [n, n] && size(a4) == [n, n]);
93 %!assert (max (a3(:)) < m && min(a3(:)) >= 0);
94 %!assert (max (a4(:)) <= m && min(a4(:)) >= -m);
95 %!assert (a5(:) == a6(:));
96
97 %!test
98 %! a = randint(10,10,-32);
99 %! assert (max(a(:)) <= 0 && min(a(:)) > -32);