1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17
18
19 //! \addtogroup fn_randi
20 //! @{
21
22
23
24 template<typename obj_type>
25 arma_warn_unused
26 inline
27 obj_type
randi(const uword n_rows,const uword n_cols,const distr_param & param=distr_param (),const typename arma_Mat_Col_Row_only<obj_type>::result * junk=nullptr)28 randi(const uword n_rows, const uword n_cols, const distr_param& param = distr_param(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk = nullptr)
29 {
30 arma_extra_debug_sigprint();
31 arma_ignore(junk);
32
33 typedef typename obj_type::elem_type eT;
34
35 if(is_Col<obj_type>::value)
36 {
37 arma_debug_check( (n_cols != 1), "randi(): incompatible size" );
38 }
39 else
40 if(is_Row<obj_type>::value)
41 {
42 arma_debug_check( (n_rows != 1), "randi(): incompatible size" );
43 }
44
45 obj_type out(n_rows, n_cols, arma_nozeros_indicator());
46
47 int a;
48 int b;
49
50 if(param.state == 0)
51 {
52 a = 0;
53 b = arma_rng::randi<eT>::max_val();
54 }
55 else
56 if(param.state == 1)
57 {
58 a = param.a_int;
59 b = param.b_int;
60 }
61 else
62 {
63 a = int(param.a_double);
64 b = int(param.b_double);
65 }
66
67 arma_debug_check( (a > b), "randi(): incorrect distribution parameters: a must be less than b" );
68
69 arma_rng::randi<eT>::fill(out.memptr(), out.n_elem, a, b);
70
71 return out;
72 }
73
74
75
76 template<typename obj_type>
77 arma_warn_unused
78 inline
79 obj_type
randi(const SizeMat & s,const distr_param & param=distr_param (),const typename arma_Mat_Col_Row_only<obj_type>::result * junk=nullptr)80 randi(const SizeMat& s, const distr_param& param = distr_param(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk = nullptr)
81 {
82 arma_extra_debug_sigprint();
83 arma_ignore(junk);
84
85 return randi<obj_type>(s.n_rows, s.n_cols, param);
86 }
87
88
89
90 template<typename obj_type>
91 arma_warn_unused
92 inline
93 obj_type
randi(const uword n_elem,const distr_param & param=distr_param (),const arma_empty_class junk1=arma_empty_class (),const typename arma_Mat_Col_Row_only<obj_type>::result * junk2=nullptr)94 randi(const uword n_elem, const distr_param& param = distr_param(), const arma_empty_class junk1 = arma_empty_class(), const typename arma_Mat_Col_Row_only<obj_type>::result* junk2 = nullptr)
95 {
96 arma_extra_debug_sigprint();
97 arma_ignore(junk1);
98 arma_ignore(junk2);
99
100 if(is_Row<obj_type>::value)
101 {
102 return randi<obj_type>(1, n_elem, param);
103 }
104 else
105 {
106 return randi<obj_type>(n_elem, 1, param);
107 }
108 }
109
110
111
112 arma_warn_unused
113 inline
114 imat
randi(const uword n_rows,const uword n_cols,const distr_param & param=distr_param ())115 randi(const uword n_rows, const uword n_cols, const distr_param& param = distr_param())
116 {
117 arma_extra_debug_sigprint();
118
119 return randi<imat>(n_rows, n_cols, param);
120 }
121
122
123
124 arma_warn_unused
125 inline
126 imat
randi(const SizeMat & s,const distr_param & param=distr_param ())127 randi(const SizeMat& s, const distr_param& param = distr_param())
128 {
129 arma_extra_debug_sigprint();
130
131 return randi<imat>(s.n_rows, s.n_cols, param);
132 }
133
134
135
136 arma_warn_unused
137 inline
138 ivec
randi(const uword n_elem,const distr_param & param=distr_param ())139 randi(const uword n_elem, const distr_param& param = distr_param())
140 {
141 arma_extra_debug_sigprint();
142
143 return randi<ivec>(n_elem, uword(1), param);
144 }
145
146
147
148 arma_warn_unused
149 inline
150 sword
randi(const distr_param & param)151 randi(const distr_param& param)
152 {
153 return as_scalar( randi<ivec>(uword(1), uword(1), param) );
154 }
155
156
157
158 template<typename eT>
159 arma_warn_unused
160 inline
161 typename arma_scalar_only<eT>::result
randi(const distr_param & param)162 randi(const distr_param& param)
163 {
164 return eT( as_scalar( randi< Col<eT> >(uword(1), uword(1), param) ) );
165 }
166
167
168
169 arma_warn_unused
170 inline
171 sword
randi()172 randi()
173 {
174 return sword( arma_rng::randi<sword>() );
175 }
176
177
178
179 template<typename eT>
180 arma_warn_unused
181 inline
182 typename arma_scalar_only<eT>::result
randi()183 randi()
184 {
185 return eT( arma_rng::randi<eT>() );
186 }
187
188
189
190 template<typename cube_type>
191 arma_warn_unused
192 inline
193 cube_type
randi(const uword n_rows,const uword n_cols,const uword n_slices,const distr_param & param=distr_param (),const typename arma_Cube_only<cube_type>::result * junk=nullptr)194 randi(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param(), const typename arma_Cube_only<cube_type>::result* junk = nullptr)
195 {
196 arma_extra_debug_sigprint();
197 arma_ignore(junk);
198
199 typedef typename cube_type::elem_type eT;
200
201 cube_type out(n_rows, n_cols, n_slices, arma_nozeros_indicator());
202
203 int a;
204 int b;
205
206 if(param.state == 0)
207 {
208 a = 0;
209 b = arma_rng::randi<eT>::max_val();
210 }
211 else
212 if(param.state == 1)
213 {
214 a = param.a_int;
215 b = param.b_int;
216 }
217 else
218 {
219 a = int(param.a_double);
220 b = int(param.b_double);
221 }
222
223 arma_debug_check( (a > b), "randi(): incorrect distribution parameters: a must be less than b" );
224
225 arma_rng::randi<eT>::fill(out.memptr(), out.n_elem, a, b);
226
227 return out;
228 }
229
230
231
232 template<typename cube_type>
233 arma_warn_unused
234 inline
235 cube_type
randi(const SizeCube & s,const distr_param & param=distr_param (),const typename arma_Cube_only<cube_type>::result * junk=nullptr)236 randi(const SizeCube& s, const distr_param& param = distr_param(), const typename arma_Cube_only<cube_type>::result* junk = nullptr)
237 {
238 arma_extra_debug_sigprint();
239 arma_ignore(junk);
240
241 return randi<cube_type>(s.n_rows, s.n_cols, s.n_slices, param);
242 }
243
244
245
246 arma_warn_unused
247 inline
248 icube
randi(const uword n_rows,const uword n_cols,const uword n_slices,const distr_param & param=distr_param ())249 randi(const uword n_rows, const uword n_cols, const uword n_slices, const distr_param& param = distr_param())
250 {
251 arma_extra_debug_sigprint();
252
253 return randi<icube>(n_rows, n_cols, n_slices, param);
254 }
255
256
257
258 arma_warn_unused
259 inline
260 icube
randi(const SizeCube & s,const distr_param & param=distr_param ())261 randi(const SizeCube& s, const distr_param& param = distr_param())
262 {
263 arma_extra_debug_sigprint();
264
265 return randi<icube>(s.n_rows, s.n_cols, s.n_slices, param);
266 }
267
268
269
270 //! @}
271