commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gilles <gil...@harfang.homelinux.org>
Subject Re: [RNG] Generating for [e]nums
Date Fri, 04 Aug 2017 19:49:51 GMT
Hi.

On Fri, 4 Aug 2017 15:29:42 -0400, Simon Spero wrote:
> On Aug 4, 2017 2:11 PM, "Gary Gregory" <garydgregory@gmail.com> 
> wrote:
>
> For example, I have a enum like:
>
> public enum CardinalDirection (NORTH,SOUTH,EAST,WEST)
>
> public CardinalDirection nextRandomDirection() {
>    return rng.next(CardinalDirection.class);
> }
>
>
> This approach may be a bit slow if the random values are near an 
> inner
> loop.
>
> In order to select a random enum value, you need to get hold of an 
> array
> containing all the possible values (equivalent to what gets returned 
> by the
> generated  "public static CardinalDirection[] values()".
>
> This requires calling Class::getEnumConstants, which does a little
> reflection, caches the result, then returns a clone of the resultant 
> array.
>
> It may be better to construct a random enum value generator object 
> that
> holds a copy of the values array, plus an RNG with the desired
> characteristics.
>
> A more general method would take an array of values, or a 
> Supplier<T[]>
> function (e.g. CardinalDirection::values).
>
> You could use a cache in the RNG, keyed by the class, but that's 
> still
> going to be a bit expensive in the middle of a tight loop.

What about the following?

---CUT---
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;

public class RandomEnum<T extends Enum<T>> {
     private final T[] values;
     private final UniformRandomProvider rand = 
RandomSource.create(RandomSource.SPLIT_MIX_64);

     public RandomEnum(Class<T> c) {
         values = c.getEnumConstants();
     }

     public T next() {
         return values[rand.nextInt(values.length)];
     }
}
---CUT---


Regards,
Gilles



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message