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:[EMAIL PROTECTED] 
        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" <[EMAIL PROTECTED]>
        > 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: [EMAIL PROTECTED]
        For additional commands, e-mail: [EMAIL PROTECTED]
        
        

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to