77
88This package explores a possible extension of ` rand ` -related
99functionalities (from the ` Random ` module); the code is initially
10- taken from https://github.com/JuliaLang/julia/pull/24912 .
10+ taken from < https://github.com/JuliaLang/julia/pull/24912 > .
1111Note that type piracy is committed!
1212While hopefully useful, this package is still experimental, and
1313hence unstable. User feedback, and design or implementation contributions are welcome.
1414
15- This does essentially 4 things:
15+ This does essentially four things:
1616
17171 ) define distribution objects, to give first-class status to features
1818 provided by ` Random ` ; for example ` rand(Normal(), 3) ` is equivalent
@@ -63,7 +63,7 @@ For convenience, the following names from `Random` are re-exported
6363in this package: ` rand! ` , ` AbstractRNG ` , ` MersenneTwister ` ,
6464` RandomDevice ` (` rand ` is in ` Base ` ). Functions like ` randn! ` or
6565` randstring ` are considered to be obsoleted by this package so are not
66- re-exported. It's still needed to import ` Random ` separately in order
66+ re-exported. It is still necessary to import ` Random ` separately in order
6767to use functions which don't extend the ` rand ` API, namely
6868` randsubseq ` , ` shuffle ` , ` randperm ` , ` randcycle ` , and their mutating
6969variants.
@@ -92,7 +92,7 @@ julia> rand(make(Pair, 1:10, Normal())) # random Pair, where both members have d
92925 => 0.674375
9393
9494julia> rand (make (Pair{Number,Any}, 1 : 10 , Normal ())) # specify the Pair type
95- Pair {Number,Any} (1 , - 0.131617 )
95+ Pair {Number, Any} (1 , - 0.131617 )
9696
9797julia> rand (Pair{Float64,Int}) # equivalent to rand(make(Pair, Float64, Int))
98980.321676 => - 4583276276690463733
@@ -122,35 +122,46 @@ julia> rand(Normal(ComplexF64)) # equivalent to randn(ComplexF64)
1221220.9322376894079347 + 0.2812214248483498im
123123
124124julia> rand (Set, 3 )
125- Set ([0.717172 , 0.78481 , 0.86901 ])
125+ Set{Float64} with 3 elements:
126+ 0.0675168818514279
127+ 0.31058418699493895
128+ 0.15029104540378424
126129
127- julia> rand! (ans, Exponential ())
128- Set ([0.7935073925105659 , 2.593684878770254 , 1.629181233597078 ])
130+ julia> rand! (ans, Exponential ())
131+ Set{Float64} with 3 elements:
132+ 1.082312697650858
133+ 1.2984094155972015
134+ 0.016146678329819485
129135
130136julia> rand (1 : 9 , Set, 3 ) # if you try `rand(1:3, Set, 9)`, it will take a while ;-)
131- Set ([3 , 5 , 8 ])
137+ Set{Int64} with 3 elements:
138+ 4
139+ 7
140+ 1
132141
133142julia> rand (Dict{String,Int8}, 2 )
134- Dict{String,Int8} with 3 entries:
143+ Dict{String, Int8} with 2 entries:
135144 " vxybIbae" => 42
136145 " bO2fTwuq" => - 13
137146
138147julia> rand (make (Pair, 1 : 9 , Normal ()), Dict, 3 )
139- Dict{Int64,Float64} with 3 entries:
148+ Dict{Int64, Float64} with 3 entries:
140149 9 => 0.916406
141150 3 => - 2.44958
142151 8 => - 0.703348
143152
153+ julia> using SparseArrays
154+
144155julia> rand (SparseVector, 0.3 , 9 ) # equivalent to sprand(9, 0.3)
145- 9 - element SparseVector{Float64,Int64} with 3 stored entries:
156+ 9 - element SparseVector{Float64, Int64} with 3 stored entries:
146157 [1 ] = 0.173858
147158 [6 ] = 0.568631
148159 [8 ] = 0.297207
149160
150161julia> rand (Normal (), SparseMatrixCSC, 0.3 , 2 , 3 ) # equivalent to sprandn(2, 3, 0.3)
151- 2 × 3 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
152- [ 2 , 1 ] = 0.448981
153- [ 1 , 2 ] = 0.730103
162+ 2 × 3 SparseMatrixCSC{Float64, Int64} with 2 stored entries:
163+ ⋅ - 1.5617 ⋅
164+ 0.572305 ⋅ ⋅
154165
155166# like for Array, sparse arrays enjoy to be special cased: `SparseVector` or `SparseMatrixCSC`
156167# can be omitted in the `rand` call (not in the `make` call):
@@ -168,26 +179,29 @@ julia> rand(make(String, 3, "123")) # ... which is as always equivalent to a cal
168179" 211"
169180
170181julia> rand (String, Set, 3 ) # String considered as a scalar
171- Set ([" 0Dfqj6Yr" , " ILngfcRz" , " HT5IEyK3" ])
182+ Set{String} with 3 elements:
183+ " jDbjXu9b"
184+ " 0Lo75VKo"
185+ " webpNhfY"
172186
173187julia> rand (BitArray, 3 ) # equivalent to, but unfortunately more verbose than, bitrand(3)
174- 3 - element BitArray{ 1 } :
175- true
176- true
177- false
178-
179- julia> julia > rand (Bernoulli (0.2 ), BitVector, 10 ) # using the Bernoulli distribution
180- 10 - element BitArray{ 1 } :
181- false
182- false
183- false
184- false
185- true
186- false
187- true
188- false
189- false
190- true
188+ 3 - element BitVector :
189+ 1
190+ 1
191+ 0
192+
193+ julia> rand (Bernoulli (0.2 ), BitVector, 10 ) # using the Bernoulli distribution
194+ 10 - element BitVector :
195+ 0
196+ 1
197+ 0
198+ 1
199+ 0
200+ 0
201+ 0
202+ 0
203+ 0
204+ 1
191205
192206julia> rand (1 : 3 , NTuple{3 }) # NTuple{3} considered as a container, equivalent to rand(make(NTuple{3}, 1:3))
193207(3 , 3 , 1 )
@@ -208,13 +222,15 @@ julia> rand(make(MVector{2,AbstractString}, String), SMatrix{3, 2})
208222 [" fFJuUtJQ" , " H2mAQrIV" ] [" pt0OYFJw" , " O0fCfjjR" ]
209223
210224julia> Set (Iterators. take (Rand (RandomDevice (), 1 : 10 ), 3 )) # RNG defaults to Random.default_rng()
211- Set ([9 , 2 , 6 ]) # note that the set could end up with less than 3 elements if `Rand` generates duplicates
225+ Set{Int64} with 2 elements: # note that the set can end up with less than 3 elements if `Rand` generates duplicates
226+ 5
227+ 9
212228
213229julia> collect (Iterators. take (Uniform (1 : 10 ), 3 )) # distributions can be iterated over, using Random.default_rng() implicitly
214- 3 - element Array {Int64, 1 }:
215- 7
216- 10
217- 5
230+ 3 - element Vector {Int64}:
231+ 9
232+ 6
233+ 8
218234
219235julia> rand (Complex => Int) # equivalent to rand(make(Complex, Int)) (experimental)
2202364610038282330316390 + 4899086469899572461im
0 commit comments