Task: Card shuffles





Java: import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class CardShuffles{

	private static final Random rand = new Random();

	public static <T> LinkedList<T> riffleShuffle(List<T> list, int flips){
		LinkedList<T> newList = new LinkedList<T>();

		newList.addAll(list);

		for(int n = 0; n < flips; n++){
			
			int cutPoint = newList.size() / 2 
				+ (rand.nextBoolean() ? -1 : 1 ) * rand.nextInt((int)(newList.size() * 0.1));

			
			List<T> left = new LinkedList<T>();
			left.addAll(newList.subList(0, cutPoint));
			List<T> right = new LinkedList<T>();
			right.addAll(newList.subList(cutPoint, newList.size()));

			newList.clear();

			while(left.size() > 0 && right.size() > 0){
				
				
				
				if(rand.nextDouble() >= ((double)left.size() / right.size()) / 2){
					newList.add(right.remove(0));
				}else{
					newList.add(left.remove(0));
				}
			}

			
			if(left.size() > 0) newList.addAll(left);
			if(right.size() > 0) newList.addAll(right);
		}
		return newList;
	}

	public static <T> LinkedList<T> overhandShuffle(List<T> list, int passes){
		LinkedList<T> mainHand = new LinkedList<T>();

		mainHand.addAll(list);
		for(int n = 0; n < passes; n++){
			LinkedList<T> otherHand = new LinkedList<T>();

			while(mainHand.size() > 0){
				
				int cutSize = rand.nextInt((int)(list.size() * 0.2)) + 1;

				LinkedList<T> temp = new LinkedList<T>();

				
				for(int i = 0; i < cutSize && mainHand.size() > 0; i++){
					temp.add(mainHand.remove());
				}

				
				if(rand.nextDouble()  >= 0.1){
					
					otherHand.addAll(0, temp);
				}else{
					
					otherHand.addAll(temp);
				}
			}

			
			mainHand = otherHand;
		}		
		return mainHand;
	}

	public static void main(String[] args){
		List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
		System.out.println(list);
		list = riffleShuffle(list, 10);
		System.out.println(list + "\n");

                list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
		System.out.println(list);
		list = riffleShuffle(list, 1);
		System.out.println(list + "\n");

		list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
		System.out.println(list);
		list = overhandShuffle(list, 10);
		System.out.println(list + "\n");

                list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
		System.out.println(list);
		list = overhandShuffle(list, 1);
		System.out.println(list + "\n");

		list = Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
		System.out.println(list);
		Collections.shuffle(list);
		System.out.println(list + "\n");
	}
}



Python: import random

def riffleShuffle(va, flips):
    nl = va
    for n in range(flips):
        
        cutPoint = len(nl)/2 + random.choice([-1, 1]) * random.randint(0, len(va)/10)

        
        left = nl[0:cutPoint]
        right = nl[cutPoint:]

        del nl[:]
        while (len(left) > 0 and len(right) > 0):
            
            
            
            if (random.uniform(0, 1) >= len(left) / len(right) / 2):
                nl.append(right.pop(0))
            else:
                nl.append(left.pop(0))
        if (len(left) > 0):
            nl = nl + left
        if (len(right) > 0):
            nl = nl + right
    return nl

def overhandShuffle(va, passes):
    mainHand = va
    for n in range(passes):
        otherHand = []
        while (len(mainHand) > 0):
            
            cutSize = random.randint(0, len(va) / 5) + 1
            temp = []

            
            i=0
            while (i<cutSize and len(mainHand) > 0):
                temp.append(mainHand.pop(0))
                i = i + 1

            
            if (random.uniform(0, 1) >= 0.1):
                
                otherHand = temp + otherHand
            else:
                otherHand = otherHand + temp
        
        mainHand = otherHand
    return mainHand

print "Riffle shuffle"
nums = [x+1 for x in range(21)]
print nums
print riffleShuffle(nums, 10)
print

print "Riffle shuffle"
nums = [x+1 for x in range(21)]
print nums
print riffleShuffle(nums, 1)
print

print "Overhand shuffle"
nums = [x+1 for x in range(21)]
print nums
print overhandShuffle(nums, 10)
print

print "Overhand shuffle"
nums = [x+1 for x in range(21)]
print nums
print overhandShuffle(nums, 1)
print

print "Library shuffle"
nums = [x+1 for x in range(21)]
print nums
random.shuffle(nums)
print nums
print