il me semble que c'est une distribution aléatoire sans retirage :
class ChaineSplitter {
// distribution aleatoire sans retirage private static String[] distribute(String[] array) { if (array != null && array.length != 0) { Random random = new Random (); random.setSeed (System.currentTimeMillis ()); for (int i = 0, t = array.length; i < t; i++) { int j = random.nextInt(t); String token = array[i]; array[i] = array[j]; array[j] = token; } } return array; } public static void main(String[]args){ StringTokenizer st = new StringTokenizer("1,2,3.4 5.6", " ,."); if (st.countTokens() != 0) { String[] myTokens = new String[st.countTokens()]; int i = 0; while (st.hasMoreTokens()) myTokens[i++] = st.nextToken(); for (i = 0; i< myTokens.length; i++) System.out.print(myTokens[i]+" "); System.out.println(""); myTokens = distribute(myTokens); for (i = 0; i< myTokens.length; i++) System.out.print(myTokens[i]+" "); } else { System.out.println("Un String please"); System.exit(1); } } }
|