-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeckGame.java
More file actions
37 lines (31 loc) · 859 Bytes
/
DeckGame.java
File metadata and controls
37 lines (31 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.ArrayList;
public class DeckGame {
public static boolean trial() {
// make a deck
ArrayList<Integer> deck = new ArrayList<Integer>();
for (int i = 0; i < 10000; i++)
deck.add(i);
// transfer cards randomly to another deck
ArrayList<Integer> transferDeck = new ArrayList<Integer>();
while (!deck.isEmpty()) {
transferDeck.add(deck.remove((int) (Math.random() * deck.size())));
}
// check
for (int i = 0; i < transferDeck.size(); i++) {
if (transferDeck.get(i) == i)
return true;
}
return false;
}
public static void main(String[] args) {
int trials = 10000;
int success = 0;
for (int i = 0; i < trials; i++) {
if (trial())
success++;
}
double prob = success * 1.0 / trials;
System.out.println(prob);
System.out.println(1 / (1 - prob));
}
}