tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Benussi" <mark_benu...@hotmail.com>
Subject RE: OT Friday - Random sorting
Date Fri, 03 Jun 2005 08:11:37 GMT
Thanks very much

-----Original Message-----
From: Rafal Krupinski [mailto:rafal.krupinski@ld.home.pl] 
Sent: 03 June 2005 09:06
To: Tomcat Users List
Subject: Re: OT Friday - Random sorting

Mark Benussi wrote:

> Hello.
> I am trying to write a comparator that randomly sorts a collection.
> I have something like this:
> 
> new Comparator() {
>       /**
> 
>        * @see java.util.Comparator#compare(java.lang.Object,
> java.lang.Object)
> 
>        */
> 
>       public int compare(Object object1, Object object2) {
>             long time = new Date().getTime();
>             Random random1 = new Random(time);
>             Integer x1 = new Integer(random1.nextInt());
>             random1.setSeed(++time);
>             Integer x2 = new Integer(random1.nextInt());
>             return x1.compareTo(x2);
>       }
> }
> 
> But sadly to no avail.
> Has anyone done this before or have any suggestions.

try this:

new Comparator(){
	Random r=new Random();//automatic seed
	
	public int compare(Object object1, Object object2){
		return r.nextInt();
	}
	public boolean equals(Object obj){return this==obj;}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message