|
1 | 1 | # RandomExtensions |
2 | 2 |
|
3 | 3 | [](https://travis-ci.org/rfourquet/RandomExtensions.jl) |
4 | | - |
5 | 4 | [](https://coveralls.io/github/rfourquet/RandomExtensions.jl?branch=master) |
6 | | - |
7 | 5 | [](http://codecov.io/github/rfourquet/RandomExtensions.jl?branch=master) |
| 6 | + |
| 7 | +This package explores a possible extension of `rand`-related |
| 8 | +functionalities (from the `Random` module); the code is initially |
| 9 | +taken from https://github.com/JuliaLang/julia/pull/24912. |
| 10 | +Note that type piracy is commited! |
| 11 | +While hopefully useful, this package is still very experimental, and |
| 12 | +hence unstable. |
| 13 | + |
| 14 | +This does mainly 3 things: |
| 15 | + |
| 16 | +1) define distribution objects, to give first-class status to features |
| 17 | + provided by `Random`; for example `rand(Normal(), 3)` is equivalent |
| 18 | + to `randn(3)`; other available distributions: `Exponential`, |
| 19 | + `CloseOpen` (for generation of floats in a close-open range), |
| 20 | + `Uniform` (which can wrap an implicit uniform distribution), |
| 21 | + `Combine` (to "combine" distribution for objects made of multiple |
| 22 | + scalars, like `Pair` or `Complex`); |
| 23 | + |
| 24 | + |
| 25 | +2) define generation of some containers filled with random values |
| 26 | + (like `Set`, `Dict`, `SparseArray`, `String`, `BitArray`); |
| 27 | + |
| 28 | +3) define a `Rand` iterator, which produces lazily random values. |
| 29 | + |
| 30 | + |
| 31 | +There is not much documentation for now: `rand`'s docstring is updated, |
| 32 | +and here are some examples: |
| 33 | + |
| 34 | +```julia |
| 35 | +julia> rand(CloseOpen()) # like rand(Float64) |
| 36 | +0.7678877639669386 |
| 37 | + |
| 38 | +julia> rand(CloseOpen(1.0, 10.0)) # generation in [1.0, 10.0) |
| 39 | +4.309057677479184 |
| 40 | + |
| 41 | +julia> rand(Normal(0.0, 10.0)) # explicit μ and σ parameters |
| 42 | +-8.473790458128912 |
| 43 | + |
| 44 | +julia> rand(Uniform(1:3)) # equivalent to rand(1:3) |
| 45 | +2 |
| 46 | + |
| 47 | +julia> rand(Combine(Pair, 1:10, Normal())) # random Pair, where both members have distinct distributions |
| 48 | +5 => 0.674375 |
| 49 | + |
| 50 | +julia> rand(Combine(Pair{Number, Any}, 1:10, Normal())) # specify the Pair type |
| 51 | +Pair{Number,Any}(1, -0.131617) |
| 52 | + |
| 53 | +julia> rand(Combine(Complex, Normal())) # each coordinate is drawn from the normal distribution |
| 54 | +1.5112317924121632 + 0.723463453534426im |
| 55 | + |
| 56 | +julia> rand(Combine(Complex, Normal(), 1:10)) # distinct distributions |
| 57 | +1.096731587266045 + 8.0im |
| 58 | + |
| 59 | +julia> rand(Set, 3) |
| 60 | +Set([0.717172, 0.78481, 0.86901]) |
| 61 | + |
| 62 | +julia> rand(1:9, Set, 3) |
| 63 | +Set([3, 5, 8]) |
| 64 | + |
| 65 | +julia> rand(Combine(Pair, 1:9, Normal()), Dict, 3) |
| 66 | +Dict{Int64,Float64} with 3 entries: |
| 67 | + 9 => 0.916406 |
| 68 | + 3 => -2.44958 |
| 69 | + 8 => -0.703348 |
| 70 | + |
| 71 | +julia> rand(0.3, 9) # equivalent to sprand(9, 0.3) |
| 72 | +9-element SparseVector{Float64,Int64} with 3 stored entries: |
| 73 | + [1] = 0.173858 |
| 74 | + [6] = 0.568631 |
| 75 | + [8] = 0.297207 |
| 76 | + |
| 77 | +julia> rand(Normal(), 0.3, 2, 3) # equivalent to sprandn(2, 3, 0.3) |
| 78 | +2×3 SparseMatrixCSC{Float64,Int64} with 2 stored entries: |
| 79 | + [2, 1] = 0.448981 |
| 80 | + [1, 2] = 0.730103 |
| 81 | + |
| 82 | +julia> rand(String, 4) # equivalent to randstring(4) |
| 83 | +"5o75" |
| 84 | + |
| 85 | +julia> rand(BitArray, 3) # equivalent to bitrand(3) |
| 86 | +3-element BitArray{1}: |
| 87 | + true |
| 88 | + true |
| 89 | + false |
| 90 | + |
| 91 | +julia> Set(Iterators.take(Rand(RandomDevice(), 1:10), 3)) # RNG defaults to Random.GLOBAL_RNG |
| 92 | +Set([9, 2, 6]) |
| 93 | + |
| 94 | +julia> collect(Iterators.take(Uniform(1:10), 3)) # distributions can be iterated over, using Random.GLOBAL_RNG implicitly |
| 95 | +3-element Array{Int64,1}: |
| 96 | + 7 |
| 97 | + 10 |
| 98 | + 5 |
| 99 | +``` |
0 commit comments