commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric Barnhill <ericbarnh...@gmail.com>
Subject Re: [lang][rng] org.apache.commons.lang3.ArrayUtils.shuffle()
Date Thu, 13 Jun 2019 17:15:01 GMT
An iterator that dynamically shuffles as you go along. That's really nice,
I had never even thought of that. Thanks.

On Thu, Jun 13, 2019 at 10:11 AM Alex Herbert <alex.d.herbert@gmail.com>
wrote:

>
> On 13/06/2019 17:56, Eric Barnhill wrote:
> > On Thu, Jun 13, 2019 at 9:36 AM sebb <sebbaz@gmail.com> wrote:
> >
> >>
> >> Rather than shuffle etc in place, how about various
> >> iterators/selectors to return entries in randomised order?
> >> [Or does that already exist?]
> >>
> > I am pretty sure random draws, and shuffling, are implemented with
> > different algorithms. Though sampling without replacement the full length
> > of the set would yield a shuffled set, I think there are more efficient
> > ways to shuffle a set.
>
> Iterators to return a random draw *without* replacement over the full
> length of the array? The iterator would dynamically shuffle the array on
> each call to next() so could be stopped early or can be called
> infinitely as if a continuous stream. Is that your idea?
>
> UniformRandomProvider rng = ...;
> int[] big = new int[1000000];
> //
> // Fill big with lots of data
> //
> IntIterator iter = ShuffleIterators.create(rng, big);
> int x = iter.next();
> int y = iter.next();
> int z = iter.next();
>
> This doesn't exist but it is easy to do. Memory requirements would
> require a copy of the data, or it could be marked as destructive to the
> input array order and shuffle in place.
>
> If you want a random draw *with* replacement then you can just call
> nextInt(int) with the size of the array to pick something.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
> For additional commands, e-mail: dev-help@commons.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message