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 enums
Date Fri, 04 Aug 2017 20:59:26 GMT
Hi Gary.

On Fri, 4 Aug 2017 11:10:57 -0700, Gary Gregory wrote:
> For example, I have a enum like:
>
> public enum CardinalDirection (NORTH,SOUTH,EAST,WEST)
>
> and I want to say
>
> traveler.travel(nextRandomDirection());
>
> where
>
> public CardinalDirection nextRandomDirection() {
>    return rng.next(CardinalDirection.class);
> }

Actually, "Commons RNG" already provides all the necessary 
functionality
for a one-liner:

---CUT---
import java.util.Arrays;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.rng.sampling.CollectionSampler;

CollectionSampler<CardinalDirection> r
     = new 
CollectionSampler<>(RandomSource.create(RandomSource.SPLIT_MIX_64),
                               
Arrays.asList(CardinalDirection.values()));

CardinalDirection e = r.sample();
---CUT---


Cheers,
Gilles


>
> Gary
>
> On Fri, Aug 4, 2017 at 11:02 AM, Amey Jadiye <ameyjadiye@gmail.com> 
> wrote:
>
>> Hi,
>>
>> What's usecase for this BTW ? Might be unaware about requirement but 
>> this
>> forced me to think why would someone need random enum ?
>>
>> Enums are generally "limited" immutable constants and people choose 
>> enum
>> over the array of constants for good reason, however random provider 
>> seems
>> best suited for choosing value from any Data-Structure holding "lot" 
>> of
>> values.
>>
>> I think it's little weird but I would be happy  if someone explain
>> advantages. :-)
>>
>> Regards,
>> Amey
>>
>> On Fri, Aug 4, 2017, 11:17 PM Gary Gregory <garydgregory@gmail.com> 
>> wrote:
>>
>> > Hi All,
>> >
>> > Any thoughts on generation when you want to the domain to be an 
>> enum?
>> >
>> > SomeEnum e = UniformRandomProvider.next(SomeEnum);
>> >
>> > ?
>> >
>> > Is that too weird for this component?
>> >
>> > Gary
>> >
>>


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


Mime
View raw message