commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Phil Steitz" <p...@steitz.com>
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
http://jakarta.apache.org/commons/math/api/org/apache/commons/math/random/RandomData.html#nextSample(java.util.Collection,
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
http://jakarta.apache.org/commons/math/api/org/apache/commons/math/random/RandomData.html#nextPermutation(int,
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:
 
http://wiki.apache.org/jakarta-commons/MathWishList
 
Phil
 

	-----Original Message----- 
	From: Stephen Colebourne [mailto:scolebourne@btopenworld.com] 
	Sent: Thu 3/10/2005 4:54 PM 
	To: Jakarta Commons Developers List 
	Cc: 
	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.
	
	Stephen
	
	
	----- Original Message -----
	From: "Hernan Silberman" <hsilberm@pdi.com>
	> 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: commons-dev-unsubscribe@jakarta.apache.org
	For additional commands, e-mail: commons-dev-help@jakarta.apache.org
	
	

Mime
View raw message