Gameshow Problem

by Hensixteen

JavaScript

car = 0;
goat = 0;

function getRandomInt(max) {
	return Math.floor(Math.random() * max);
}

runs = 1000000
for (i = 0; i < runs; i++) {
	doors = ['g', 'g', 'g'];
  doorStatus = [0, 0, 0]; // 0 = closed, 1 = open
  doors[getRandomInt(3)] = 'c';
	
 	choice = getRandomInt(3);
  
  for (j = 0; j < doors.length; j++) {
  	if ((doors[j] == 'g') && (j != choice)) {
    	doorStatus[j] = 1;
      break;
    }
  }
  for (j = 0; j < doors.length; j++) {
  	if ((doorStatus[j] == 0) && (j != choice)) {
    	choice = j;
      break;
    }
  }
  
  if (doors[choice] == 'c') {
  	car++;
  }
  else {
  	goat++;
  }
}

console.log("Cars = " + car + ", Goats = " + goat + ", pct = " + (car/runs)*100);