commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Phil Steitz" <>
Subject RE: [Collections] Random iterator?
Date Fri, 11 Mar 2005 16:25:46 GMT
You can do this kind of thing using some methods in the random package in [math]. See,
int) for a way to sample from a collection.  By using sample size = 1, you can select a single
random element.
You can iterate over a list in a random order by first generating a random permutation using,
int) and then using the resulting int[] array to select elements.
If random lists generated from collections are of interest, we could add a method to do this
directly.  You can post any ideas on improving / extending commons math here:

	-----Original Message----- 
	From: Stephen Colebourne [] 
	Sent: Thu 3/10/2005 4:54 PM 
	To: Jakarta Commons Developers List 
	Subject: Re: [Collections] Random iterator?

	We don't have such a method or iterator at present. I would like to keep
	[collections] for deterministic not random effects if possible.
	----- Original Message -----
	From: "Hernan Silberman" <>
	> In my unit and load tests, I often find myself retrieving a collection of
	> objects from a service, and selecting a random one from the collection.
	> Seems
	> like a common use case (for testing anyhow) and I don't think there's any
	> support in java.util Collections for, say, fetching a random item out of a
	> Set.
	> It's easy enough to code by hand, but I was wondering if it's already done
	> somewhere in commons-collections.  A Random iterator would be really cool.
	> I suppose:
	> List theList = new ArrayList( someCollection );
	> Collections.shuffle( theList );
	> Iterator theIt = theList.iterator();
	> works fine too..
	> thnx...
	> hernan
	To unsubscribe, e-mail:
	For additional commands, e-mail:

View raw message