@@ -146,7 +146,7 @@ rand{T<:Number}(::Type{T}) = error("no random number generator for type $T; try
146
146
rand {T<:Number} (:: Type{T} , dims:: Int... ) = rand (T, dims)
147
147
148
148
149
- # # Generate random integer within a range
149
+ # # Generate random integer within a range 1:n
150
150
151
151
# remainder function according to Knuth, where rem_knuth(a, 0) = a
152
152
rem_knuth (a:: Uint , b:: Uint ) = a % (b + (b == 0 )) + a * (b == 0 )
@@ -208,18 +208,24 @@ function rand{T<:Integer, U<:Unsigned}(g::RandIntGen{T,U})
208
208
itrunc (T, one (U) + rem_knuth (x, g. k))
209
209
end
210
210
211
- rand {T} (r:: Range{T} ) = r[rand (randintgen (length (r)))]
212
211
213
- function rand! (r:: Range , A:: AbstractArray )
212
+ # # Randomly draw a sample from an AbstractArray r
213
+ # (e.g. r is a range 0:2:8 or a vector [2, 3, 5, 7])
214
+
215
+ rand (r:: AbstractArray ) = r[rand (randintgen (length (r)))]
216
+
217
+ # Arrays of random integers
218
+
219
+ function rand! (r:: AbstractArray , A:: AbstractArray )
214
220
g = randintgen (length (r))
215
221
for i = 1 : length (A)
216
222
@inbounds A[i] = r[rand (g)]
217
223
end
218
224
return A
219
225
end
220
226
221
- rand {T} (r:: Range {T} , dims:: Dims ) = rand! (r, Array (T, dims))
222
- rand (r:: Range , dims:: Int... ) = rand (r, dims)
227
+ rand {T} (r:: AbstractArray {T} , dims:: Dims ) = rand! (r, Array (T, dims))
228
+ rand (r:: AbstractArray , dims:: Int... ) = rand (r, dims)
223
229
224
230
225
231
# # random Bools
0 commit comments