How to pick random values from an `Enum` type?

singularitti
Wed, 16 Nov 2022 07:47:52 +0000
General Usage

For example, define type Ball:

@enum Ball b=1 r=-1

julia> rand(Ball, 10)
ERROR: ArgumentError: Sampler for this object is not defined
Stacktrace:
 [1] Random.Sampler(#unused#::Type{Random.TaskLocalRNG}, sp::Random.SamplerType{Ball}, #unused#::Val{1})
   @ Random ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:146
 [2] Random.Sampler(rng::Random.TaskLocalRNG, x::Random.SamplerType{Ball}, r::Val{1})
   @ Random ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:140
 [3] rand(rng::Random.TaskLocalRNG, X::Random.SamplerType{Ball})
   @ Random ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:254
 [4] rand!
   @ ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:272 [inlined]
 [5] rand!
   @ ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:268 [inlined]
 [6] rand
   @ ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:289 [inlined]
 [7] rand
   @ ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:290 [inlined]
 [8] rand(::Type{Ball}, ::Int64)
   @ Random ~/.julia/juliaup/julia-1.8.2+0.x64/share/julia/stdlib/v1.8/Random/src/Random.jl:293
 [9] top-level scope
   @ REPL[2]:1

2 posts - 2 participants