0
|
1 ## Copyright (C) 2001 Laurent Mazet |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## usage: b = randint (n, [m, [range, [seed]]]) |
|
18 ## |
|
19 ## Generate a matrix of random binary numbers. |
|
20 ## |
|
21 ## n: number of rows. |
|
22 ## m: number of columns (by default m is equal to n). |
|
23 ## range: range of random numbers. if range is a scalar the integer range |
|
24 ## is [0, range-1], else, the ouput range is [range(1), range(2)]. |
|
25 ## seed: seed of the random generator. |
|
26 ## |
|
27 ## b : random matrix. |
|
28 |
|
29 ## 2001 FEB 07 |
|
30 ## initial release |
|
31 |
|
32 function b = randint (n, m, range, seed) |
|
33 |
|
34 switch (nargin) |
|
35 case 1, |
|
36 m = n; |
|
37 range = [0 1]; |
|
38 seed = Inf; |
|
39 case 2, |
|
40 range = [0 1]; |
|
41 seed = Inf; |
|
42 case 3, |
|
43 seed = Inf; |
|
44 otherwise |
|
45 error ("b = randint (n, [m, [range, [seed]]])"); |
|
46 endswitch |
|
47 |
|
48 ## Check range |
|
49 if (length (range) == 1) |
|
50 range = [0, range-1]; |
|
51 elseif ( prod (size (range)) != 2) |
|
52 error ("randint: range must be a 2 element vector"); |
|
53 endif |
|
54 range = sort (range); |
|
55 |
|
56 ## Check seed; |
|
57 if (!isinf (seed)) |
|
58 old_seed = rand ("seed"); |
|
59 rand ("seed", seed); |
|
60 endif |
|
61 |
|
62 b = range (1) - 1 + ceil (rand (n, m) * (range (2) - range (1) + 1)); |
|
63 |
|
64 ## Get back to the old |
|
65 if (!isinf (seed)) |
|
66 rand ("seed", old_seed); |
|
67 endif |
|
68 |
|
69 endfunction |