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

**URL:** <https://discourse.julialang.org/t/how-to-pick-random-values-from-an-enum-type/90330>\
**Category:** General Usage\
**Tags:** random, enum\
**Created:** [November 16, 2022, 7:47am UTC](https://discourse.julialang.org/t/how-to-pick-random-values-from-an-enum-type/90330 "2022-11-16T07:47:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [November 16, 2022, 7:47am UTC](https://discourse.julialang.org/t/how-to-pick-random-values-from-an-enum-type/90330/1 "2022-11-16T07:47:52Z")

</div>

For example, define type `Ball`:

```julia
@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

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [November 16, 2022, 8:11am UTC](https://discourse.julialang.org/t/how-to-pick-random-values-from-an-enum-type/90330/2 "2022-11-16T08:11:44Z")

</div>

This works:

```julia
rand(instances(Ball), 10)

```
