Skip to main content
edited tags; edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
added 1 character in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

which of the Comparing two shuffling method is better?methods

I'm trying to randomly shuffle an collection of integers within a ListList and I have came up with two shuffling method that does the job. However, I'm not sure which one works better?. Does any one have any comments or suggestions?

public class TheCollectionInterface {

    public static <E> void swap(List<E> list1, int i, int j){
        E temp = list1.get(i);
        list1.set(i, list1.get(j));
        list1.set(j, temp);
    }
    
//alternative version    
//    public static void shuffling(List<?> list, Random rnd){
//        for(int i = list.size(); i >= 1; i--){
//            swap(list, i - 1, rnd.nextInt(i));
//        }
//    }
        
    
    public static <E> void shuffling(List<E> list1, Random rnd){
        for(int i = list1.size(); i >= 1; i--){
            swap(list1, i - 1, rnd.nextInt(list1.size()));
        }
    }

    public static void main(String[] args) {
        
        List<Integer> li2 = Arrays.asList(1,2,3,4,5,6,7,8,9);
        
        Random r1 = new Random();
        
        TheCollectionInterface.shuffling(li2, r1);
        
        System.out.println(li2);
    }
}

which of the two shuffling method is better?

I'm trying to randomly shuffle an collection of integers within a List and I have came up with two shuffling method that does the job. However, I'm not sure which one works better? Does any one have any comments or suggestions?

public class TheCollectionInterface {

    public static <E> void swap(List<E> list1, int i, int j){
        E temp = list1.get(i);
        list1.set(i, list1.get(j));
        list1.set(j, temp);
    }
    
//alternative version    
//    public static void shuffling(List<?> list, Random rnd){
//        for(int i = list.size(); i >= 1; i--){
//            swap(list, i - 1, rnd.nextInt(i));
//        }
//    }
        
    
    public static <E> void shuffling(List<E> list1, Random rnd){
        for(int i = list1.size(); i >= 1; i--){
            swap(list1, i - 1, rnd.nextInt(list1.size()));
        }
    }

    public static void main(String[] args) {
        
        List<Integer> li2 = Arrays.asList(1,2,3,4,5,6,7,8,9);
        
        Random r1 = new Random();
        
        TheCollectionInterface.shuffling(li2, r1);
        
        System.out.println(li2);
    }
}

Comparing two shuffling methods

I'm trying to randomly shuffle an collection of integers within a List and I have came up with two shuffling method that does the job. However, I'm not sure which one works better. Does any one have any comments or suggestions?

public class TheCollectionInterface {

    public static <E> void swap(List<E> list1, int i, int j){
        E temp = list1.get(i);
        list1.set(i, list1.get(j));
        list1.set(j, temp);
    }
    
//alternative version    
//    public static void shuffling(List<?> list, Random rnd){
//        for(int i = list.size(); i >= 1; i--){
//            swap(list, i - 1, rnd.nextInt(i));
//        }
//    }
        
    
    public static <E> void shuffling(List<E> list1, Random rnd){
        for(int i = list1.size(); i >= 1; i--){
            swap(list1, i - 1, rnd.nextInt(list1.size()));
        }
    }

    public static void main(String[] args) {
        
        List<Integer> li2 = Arrays.asList(1,2,3,4,5,6,7,8,9);
        
        Random r1 = new Random();
        
        TheCollectionInterface.shuffling(li2, r1);
        
        System.out.println(li2);
    }
}
Source Link
Thor
  • 627
  • 1
  • 9
  • 14

which of the two shuffling method is better?

I'm trying to randomly shuffle an collection of integers within a List and I have came up with two shuffling method that does the job. However, I'm not sure which one works better? Does any one have any comments or suggestions?

public class TheCollectionInterface {

    public static <E> void swap(List<E> list1, int i, int j){
        E temp = list1.get(i);
        list1.set(i, list1.get(j));
        list1.set(j, temp);
    }
    
//alternative version    
//    public static void shuffling(List<?> list, Random rnd){
//        for(int i = list.size(); i >= 1; i--){
//            swap(list, i - 1, rnd.nextInt(i));
//        }
//    }
        
    
    public static <E> void shuffling(List<E> list1, Random rnd){
        for(int i = list1.size(); i >= 1; i--){
            swap(list1, i - 1, rnd.nextInt(list1.size()));
        }
    }

    public static void main(String[] args) {
        
        List<Integer> li2 = Arrays.asList(1,2,3,4,5,6,7,8,9);
        
        Random r1 = new Random();
        
        TheCollectionInterface.shuffling(li2, r1);
        
        System.out.println(li2);
    }
}