[RNG] Generating for nums

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[RNG] Generating for nums

garydgregory
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RNG] Generating for nums

Amey Jadiye
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 <[hidden email]> 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
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RNG] Generating for nums

garydgregory
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);
}

Gary

On Fri, Aug 4, 2017 at 11:02 AM, Amey Jadiye <[hidden email]> 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 <[hidden email]> 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
> >
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RNG] Generating for nums

Amey Jadiye
That convinced me +1. rng or lang both feels good to me.

Regards,
Amey

On Fri, Aug 4, 2017 at 11:40 PM, Gary Gregory <[hidden email]>
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);
> }
>
> Gary
>
> On Fri, Aug 4, 2017 at 11:02 AM, Amey Jadiye <[hidden email]> 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 <[hidden email]>
> 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: [hidden email]

For additional commands, e-mail: [hidden email]
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RNG] Generating for enums

Gilles Sadowski
In reply to this post by garydgregory
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 <[hidden email]>
> 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 <[hidden email]>
>> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RNG] Generating for enums

garydgregory
On Fri, Aug 4, 2017 at 1:59 PM, Gilles <[hidden email]> wrote:

> 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---
>

Very nice, thank you.

I can see a wrapper for this boilerplate though:

public class RandomEnum<E> {

private final Class<E> enumClass;
private final CollectionSampler<CellDirection> rng;

public RandomEnum(final Class<E> enumClass) {
super();
this.enumClass = enumClass;
this.rng = new
CollectionSampler<>(RandomSource.create(RandomSource.SPLIT_MIX_64),
Arrays.asList(CellDirection.values()));

}

public CellDirection next() {
return rng.sample();
}

@Override
public String toString() {
return "RandomEnum [rng=" + rng + ", enumClass=" + enumClass + "]";
}
}

Gary



>
> Cheers,
> Gilles
>
>
>
>> Gary
>>
>> On Fri, Aug 4, 2017 at 11:02 AM, Amey Jadiye <[hidden email]>
>> 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 <[hidden email]>
>>> 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RNG] Generating for enums

Gilles Sadowski
On Sat, 5 Aug 2017 09:47:20 -0700, Gary Gregory wrote:

> On Fri, Aug 4, 2017 at 1:59 PM, Gilles <[hidden email]>
> wrote:
>
>> 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---
>>
>
> Very nice, thank you.
>
> I can see a wrapper for this boilerplate though:
>
> public class RandomEnum<E> {
>
> private final Class<E> enumClass;
> private final CollectionSampler<CellDirection> rng;
>
> public RandomEnum(final Class<E> enumClass) {
> super();
> this.enumClass = enumClass;
> this.rng = new
> CollectionSampler<>(RandomSource.create(RandomSource.SPLIT_MIX_64),
> Arrays.asList(CellDirection.values()));
>
> }
>
> public CellDirection next() {
> return rng.sample();
> }
>
> @Override
> public String toString() {
> return "RandomEnum [rng=" + rng + ", enumClass=" + enumClass + "]";
> }
> }

I guess there is a mix of generics and "specifics" here (cf.
"CellDirection"). And if you have only have a generic "E" in
that class, you won't be able to call "E.values()".
Then see also my other post:
   http://markmail.org/message/h63yb7ozk4nyjagj

However, what does one gain with having to call this one-liner:
   RandomEnum<SomeEnum> r =
       new RandomEnum<SomeEnum>(SomeEnum.class,
                               
RandomSource.create(RandomSource.SPLIT_MIX_64));
vs the one I've suggested above?

Gilles

>> [...]

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [RNG] Generating for enums

garydgregory
Yeah, sorry, I forgot about the hard coded enuf.

Gary

On Aug 5, 2017 11:37 AM, "Gilles" <[hidden email]> wrote:

> On Sat, 5 Aug 2017 09:47:20 -0700, Gary Gregory wrote:
>
>> On Fri, Aug 4, 2017 at 1:59 PM, Gilles <[hidden email]>
>> wrote:
>>
>> 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(CardinalDirectio
>>> n.values()));
>>>
>>> CardinalDirection e = r.sample();
>>> ---CUT---
>>>
>>>
>> Very nice, thank you.
>>
>> I can see a wrapper for this boilerplate though:
>>
>> public class RandomEnum<E> {
>>
>> private final Class<E> enumClass;
>> private final CollectionSampler<CellDirection> rng;
>>
>> public RandomEnum(final Class<E> enumClass) {
>> super();
>> this.enumClass = enumClass;
>> this.rng = new
>> CollectionSampler<>(RandomSource.create(RandomSource.SPLIT_MIX_64),
>> Arrays.asList(CellDirection.values()));
>>
>> }
>>
>> public CellDirection next() {
>> return rng.sample();
>> }
>>
>> @Override
>> public String toString() {
>> return "RandomEnum [rng=" + rng + ", enumClass=" + enumClass + "]";
>> }
>> }
>>
>
> I guess there is a mix of generics and "specifics" here (cf.
> "CellDirection"). And if you have only have a generic "E" in
> that class, you won't be able to call "E.values()".
> Then see also my other post:
>   http://markmail.org/message/h63yb7ozk4nyjagj
>
> However, what does one gain with having to call this one-liner:
>   RandomEnum<SomeEnum> r =
>       new RandomEnum<SomeEnum>(SomeEnum.class,
>                                RandomSource.create(RandomSou
> rce.SPLIT_MIX_64));
> vs the one I've suggested above?
>
> Gilles
>
> [...]
>>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Loading...