Created
March 31, 2020 06:39
-
-
Save joriki/0101f1d6672faf32ed71926b47aa0c96 to your computer and use it in GitHub Desktop.
Simulate the expected number of suit matches in pairs of consecutive cards in a standard 52-card deck; see https://math.stackexchange.com/questions/3602087.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class Question3602087 { | |
final static long ntrials = 10000000; | |
final static Random random = new Random(); | |
final static int NSUITS = 4; | |
final static int NRANKS = 13; | |
final static int NCARDS = NRANKS * NSUITS; | |
public static void main(String [] args) { | |
int [] deck = new int [NCARDS]; | |
for (int i = 0;i < NCARDS;i++) | |
deck [i] = i % NSUITS; | |
long s = 0; | |
long s2 = 0; | |
for (long n = 0;n < ntrials;n++) { | |
int last = 0; | |
long count = 0; | |
for (int i = 0,next = NCARDS;i < NCARDS;i++) { | |
int pos = random.nextInt(next--); | |
int card = deck [pos]; | |
deck [pos] = deck [next]; | |
deck [next] = card; | |
if (i != 0 && card == last) | |
count++; | |
last = card; | |
} | |
s += count; | |
s2 += count * count; | |
} | |
double e = s / (double) ntrials; | |
double e2 = s2 / (double) ntrials; | |
System.out.println("mean: " + e); | |
System.out.println("variance: " + (e2 - e * e)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment