Created
January 21, 2018 17:19
-
-
Save JimmyBlack7/4053affefadfa305d2ac34bb5edc7f46 to your computer and use it in GitHub Desktop.
randomness simulation
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
using System.Collections.Generic; | |
using UnityEngine; | |
public class pigeon : MonoBehaviour | |
{ | |
public int numberOfTests = 1000; | |
private int pickedGate; | |
private int goodGate; | |
private int numberOfWinsNoChange = 0; | |
private int numberOfWinsChange = 0; | |
private int gateToReject; | |
private void Start() | |
{ | |
Debug.Log("Press A for MontyHall paradox simulation"); | |
Debug.Log("Press B for 5 coin tosses simulation"); | |
} | |
void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.A)) | |
{ | |
//test without changing a gate | |
for (int i = 1; i < numberOfTests; i++) | |
{ | |
pickedGate = Random.Range(1, 4); | |
goodGate = Random.Range(1, 4); | |
if (pickedGate == goodGate) | |
{ | |
numberOfWinsNoChange++; | |
} | |
if (i == numberOfTests - 1) | |
{ | |
Debug.Log("Number of wins without change" + numberOfWinsNoChange); | |
numberOfWinsNoChange = 0; | |
} | |
} | |
//test with a change | |
for (int j = 1; j < numberOfTests; j++) | |
{ | |
List<int> numbersToChooseFrom = new List<int>(new int[] { 1, 2, 3 }); | |
List<int> gateToRejectList = new List<int>(new int[] { 1, 2, 3 }); | |
pickedGate = Random.Range(1, 4); | |
goodGate = Random.Range(1, 4); | |
gateToRejectList.Remove(pickedGate); | |
gateToRejectList.Remove(goodGate); | |
gateToReject = gateToRejectList[0]; | |
numbersToChooseFrom.Remove(gateToReject); | |
numbersToChooseFrom.Remove(pickedGate); | |
pickedGate = numbersToChooseFrom[0]; | |
if (pickedGate == goodGate) | |
{ | |
numberOfWinsChange++; | |
} | |
if (j == numberOfTests - 1) | |
{ | |
Debug.Log("Number of wins with change" + numberOfWinsChange); | |
numberOfWinsChange = 0; | |
} | |
numbersToChooseFrom.Clear(); | |
gateToRejectList.Clear(); | |
} | |
} | |
//coin drops | |
if (Input.GetKeyDown(KeyCode.B)) | |
{ | |
for (int h = 1; h < numberOfTests; h++) | |
{ | |
int[] coinDrops = new int[5]; | |
for (int k = 1; k < coinDrops.Length; k++) | |
{ | |
coinDrops[k] = Random.Range(0, 2); | |
} | |
Debug.Log(coinDrops[0] + " , " + coinDrops[1] + " , " + coinDrops[2] + " , " + coinDrops[3] + " , " + coinDrops[4]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment