Skip to content

Instantly share code, notes, and snippets.

@joriki
Created March 15, 2012 15:53
Show Gist options
  • Save joriki/2044911 to your computer and use it in GitHub Desktop.
Save joriki/2044911 to your computer and use it in GitHub Desktop.
Brownian Backgammon simulation to test doubling strategies
import java.util.Random;
public class Question118566 {
public static void main (String [] args) {
double lambda0 = Double.parseDouble (args [0]);
double k0 = Double.parseDouble (args [1]);
double lambda1 = Double.parseDouble (args [2]);
double k1 = Double.parseDouble (args [3]);
int ntrials = 100000;
final int L = 100;
int sum0 = 0;
int sum1 = 0;
Random random = new Random (0);
for (int n = 0;n < ntrials;n++) {
int pos = L / 2;
int owner = -1;
int value = 1;
for (;;) {
if (random.nextBoolean ())
pos++;
else
pos--;
if (pos == 0) {
sum0 += value;
break;
}
if (pos == L) {
sum1 += value;
break;
}
if (owner == -1) {
if (pos <= L * (1 - lambda0)) {
// player 0 tries to double
if (pos <= L * (1 - lambda1)) {
// player 1 resigns
sum0 += value;
break;
}
// player 1 accepts
value *= 2;
owner = 1;
}
else if (pos >= L * lambda1) {
// player 1 tries to double
if (pos >= L * lambda0) {
// player 0 resigns
sum1 += value;
break;
}
// player 0 accepts
value *= 2;
owner = 0;
}
}
else if (owner == 0) {
if (pos <= L * (1 - k0)) {
// player 0 tries to double
if (pos <= L * (1 - k1)) {
// player 1 resigns
sum0 += value;
break;
}
// player 1 accepts
value *= 2;
owner = 1;
}
}
else { // owner == 1
if (pos >= L * k1) {
// player 1 tries to double
if (pos >= L * k0) {
// player 0 resigns
sum1 += value;
break;
}
// player 0 accepts
value *= 2;
owner = 0;
}
}
}
}
System.out.println ("points for 0 : " + sum0);
System.out.println ("points for 1 : " + sum1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment