You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Duncan Jones <du...@wortharead.com> on 2013/06/13 10:03:20 UTC
[LANG] Method to return random byte array?
Hi,
A piece of code I write far too often is:
======
public static byte[] getRandomBytes(int length) {
// validate length is non-negative
byte[] result = new byte[length];
RANDOM.nextBytes(result);
return result;
}
======
Is there a place for something like this in Commons Lang? Or, better,
does this already exist in another commons project?
Duncan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [LANG] Method to return random byte array?
Posted by Duncan Jones <du...@wortharead.com>.
On 15 June 2013 14:58, Duncan Jones <du...@wortharead.com> wrote:
> On 14 June 2013 07:55, Duncan Jones <du...@wortharead.com> wrote:
>> On 13 June 2013 21:26, Phil Steitz wrote:
>>> On 6/13/13 1:03 AM, Duncan Jones wrote:
> <snip>
>>
>>> I am not sure where this little method would fit in [lang]; but it might
>>> make sense to add there in the simple form above (with RANDOM
>>> static, I assume).
>>
>> Indeed, I had assumed any addition to [lang] would just use a standard
>> source of randomness (i.e. a static Random or SecureRandom instance).
>> However, I'm struggling to see an existing class that could
>> accommodate the functionality and creating a new "RandomUtils" class
>> feels a bit heavyweight, unless others can think of additional methods
>> that would be useful.
>
> I gave it some further thought and realised there were some other
> methods that would prove useful in such a "RandomUtils" class. I've
> created https://issues.apache.org/jira/browse/LANG-900 and attached an
> implementation. I welcome comments.
In particular, I welcome comments on whether or not to constrain the
start and end range values to be non-negative.
In the current implementation (attached to the bug), I enforced
non-negative values, e.g. for `nextInt(X, Y)`, both X and Y must be
non-negative. This simplified the implementation because it meant that
"Y - X" couldn't be larger than the maximum value of an int. However,
this could be changed for nextInt with (untested) code such as:
public static int nextInt(int startInclusive, int endExclusive) {
Validate.isTrue(endExclusive >= startInclusive,
"Start value must be smaller or equal to end value.");
if (startInclusive == endExclusive) {
return startInclusive;
}
long range = endExclusive - startInclusive;
return (int) startInclusive + nextLong(0, range);
}
This may work for ints, but it gets more difficult for longs. Any
thoughts on this?
Duncan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [LANG] Method to return random byte array?
Posted by Gary Gregory <ga...@gmail.com>.
On Sat, Jun 15, 2013 at 12:35 PM, Duncan Jones <du...@wortharead.com>wrote:
> On 15 June 2013 15:49, Gary Gregory <ga...@gmail.com> wrote:
> > I thought 2.6 had such a class and we got rid of it.
>
> 2.6 had a class by the same name (so I've discovered since your mail),
> but it merely amalgamated the different random methods from Math and
> Random.
>
> I think this new class does exactly what is required from Commons Lang
> and adds functionality that is missing from the base classes.
>
Ok, well, I just want to check that we are not adding back what er removed
;)
Gary
>
> Duncan
>
> >
> >
> > On Sat, Jun 15, 2013 at 9:58 AM, Duncan Jones <du...@wortharead.com>
> wrote:
> >
> >> On 14 June 2013 07:55, Duncan Jones <du...@wortharead.com> wrote:
> >> > On 13 June 2013 21:26, Phil Steitz wrote:
> >> >> On 6/13/13 1:03 AM, Duncan Jones wrote:
> >> <snip>
> >> >
> >> >> I am not sure where this little method would fit in [lang]; but it
> might
> >> >> make sense to add there in the simple form above (with RANDOM
> >> >> static, I assume).
> >> >
> >> > Indeed, I had assumed any addition to [lang] would just use a standard
> >> > source of randomness (i.e. a static Random or SecureRandom instance).
> >> > However, I'm struggling to see an existing class that could
> >> > accommodate the functionality and creating a new "RandomUtils" class
> >> > feels a bit heavyweight, unless others can think of additional methods
> >> > that would be useful.
> >>
> >> I gave it some further thought and realised there were some other
> >> methods that would prove useful in such a "RandomUtils" class. I've
> >> created https://issues.apache.org/jira/browse/LANG-900 and attached an
> >> implementation. I welcome comments.
> >>
> >> Duncan
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> >> For additional commands, e-mail: dev-help@commons.apache.org
> >>
> >>
> >
> >
> > --
> > E-Mail: garydgregory@gmail.com | ggregory@apache.org
> > Java Persistence with Hibernate, Second Edition<
> http://www.manning.com/bauer3/>
> > JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> > Spring Batch in Action <http://www.manning.com/templier/>
> > Blog: http://garygregory.wordpress.com
> > Home: http://garygregory.com/
> > Tweet! http://twitter.com/GaryGregory
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
--
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory
Re: [LANG] Method to return random byte array?
Posted by Duncan Jones <du...@wortharead.com>.
On 15 June 2013 15:49, Gary Gregory <ga...@gmail.com> wrote:
> I thought 2.6 had such a class and we got rid of it.
2.6 had a class by the same name (so I've discovered since your mail),
but it merely amalgamated the different random methods from Math and
Random.
I think this new class does exactly what is required from Commons Lang
and adds functionality that is missing from the base classes.
Duncan
>
>
> On Sat, Jun 15, 2013 at 9:58 AM, Duncan Jones <du...@wortharead.com> wrote:
>
>> On 14 June 2013 07:55, Duncan Jones <du...@wortharead.com> wrote:
>> > On 13 June 2013 21:26, Phil Steitz wrote:
>> >> On 6/13/13 1:03 AM, Duncan Jones wrote:
>> <snip>
>> >
>> >> I am not sure where this little method would fit in [lang]; but it might
>> >> make sense to add there in the simple form above (with RANDOM
>> >> static, I assume).
>> >
>> > Indeed, I had assumed any addition to [lang] would just use a standard
>> > source of randomness (i.e. a static Random or SecureRandom instance).
>> > However, I'm struggling to see an existing class that could
>> > accommodate the functionality and creating a new "RandomUtils" class
>> > feels a bit heavyweight, unless others can think of additional methods
>> > that would be useful.
>>
>> I gave it some further thought and realised there were some other
>> methods that would prove useful in such a "RandomUtils" class. I've
>> created https://issues.apache.org/jira/browse/LANG-900 and attached an
>> implementation. I welcome comments.
>>
>> Duncan
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
>> For additional commands, e-mail: dev-help@commons.apache.org
>>
>>
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [LANG] Method to return random byte array?
Posted by Gary Gregory <ga...@gmail.com>.
I thought 2.6 had such a class and we got rid of it.
G
On Sat, Jun 15, 2013 at 9:58 AM, Duncan Jones <du...@wortharead.com> wrote:
> On 14 June 2013 07:55, Duncan Jones <du...@wortharead.com> wrote:
> > On 13 June 2013 21:26, Phil Steitz wrote:
> >> On 6/13/13 1:03 AM, Duncan Jones wrote:
> <snip>
> >
> >> I am not sure where this little method would fit in [lang]; but it might
> >> make sense to add there in the simple form above (with RANDOM
> >> static, I assume).
> >
> > Indeed, I had assumed any addition to [lang] would just use a standard
> > source of randomness (i.e. a static Random or SecureRandom instance).
> > However, I'm struggling to see an existing class that could
> > accommodate the functionality and creating a new "RandomUtils" class
> > feels a bit heavyweight, unless others can think of additional methods
> > that would be useful.
>
> I gave it some further thought and realised there were some other
> methods that would prove useful in such a "RandomUtils" class. I've
> created https://issues.apache.org/jira/browse/LANG-900 and attached an
> implementation. I welcome comments.
>
> Duncan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
--
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory
Re: [LANG] Method to return random byte array?
Posted by Duncan Jones <du...@wortharead.com>.
On 14 June 2013 07:55, Duncan Jones <du...@wortharead.com> wrote:
> On 13 June 2013 21:26, Phil Steitz wrote:
>> On 6/13/13 1:03 AM, Duncan Jones wrote:
<snip>
>
>> I am not sure where this little method would fit in [lang]; but it might
>> make sense to add there in the simple form above (with RANDOM
>> static, I assume).
>
> Indeed, I had assumed any addition to [lang] would just use a standard
> source of randomness (i.e. a static Random or SecureRandom instance).
> However, I'm struggling to see an existing class that could
> accommodate the functionality and creating a new "RandomUtils" class
> feels a bit heavyweight, unless others can think of additional methods
> that would be useful.
I gave it some further thought and realised there were some other
methods that would prove useful in such a "RandomUtils" class. I've
created https://issues.apache.org/jira/browse/LANG-900 and attached an
implementation. I welcome comments.
Duncan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [LANG] Method to return random byte array?
Posted by Duncan Jones <du...@wortharead.com>.
On 13 June 2013 21:26, Phil Steitz wrote:
> On 6/13/13 1:03 AM, Duncan Jones wrote:
>> Hi,
>>
>> A piece of code I write far too often is:
>>
>> ======
>> public static byte[] getRandomBytes(int length) {
>> // validate length is non-negative
>>
>> byte[] result = new byte[length];
>> RANDOM.nextBytes(result);
>> return result;
>> }
>> ======
>>
>> Is there a place for something like this in Commons Lang? Or, better,
>> does this already exist in another commons project?
>
> I would be +1 for adding this (modified to use the configured
> RandomGenerator) to the RandomDataGenerator class in
> o.a.c.math.random. The advantage of adding it to there is that that
> class allows the underlying PRNG to be configured by the user.
That looks like a good place to put it. I'll submit a patch to that project.
> I am not sure where this little method would fit in [lang]; but it might
> make sense to add there in the simple form above (with RANDOM
> static, I assume).
Indeed, I had assumed any addition to [lang] would just use a standard
source of randomness (i.e. a static Random or SecureRandom instance).
However, I'm struggling to see an existing class that could
accommodate the functionality and creating a new "RandomUtils" class
feels a bit heavyweight, unless others can think of additional methods
that would be useful.
Duncan
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org
Re: [LANG] Method to return random byte array?
Posted by Phil Steitz <ph...@gmail.com>.
On 6/13/13 1:03 AM, Duncan Jones wrote:
> Hi,
>
> A piece of code I write far too often is:
>
> ======
> public static byte[] getRandomBytes(int length) {
> // validate length is non-negative
>
> byte[] result = new byte[length];
> RANDOM.nextBytes(result);
> return result;
> }
> ======
>
> Is there a place for something like this in Commons Lang? Or, better,
> does this already exist in another commons project?
I would be +1 for adding this (modified to use the configured
RandomGenerator) to the RandomDataGenerator class in
o.a.c.math.random. The advantage of adding it to there is that that
class allows the underlying PRNG to be configured by the user. I am
not sure where this little method would fit in [lang]; but it might
make sense to add there in the simple form above (with RANDOM
static, I assume).
Phil
>
> Duncan
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org