i have a game where the exit will trigger a kind of dice which can lead you to a random level. I have this script that does this:
public string levelToLoad; // first level
public bool levelSplit; //Do we split?
public string levelToSplit; //second levl
public int splitLevelChance; //if the chance was 4/7, this should be the 4
public int SplitDenominator; //And this should be the 7
int normal = 0; //To keep count
int split = 0;
for (int i = 0; i < 2000; i++) //Run the test 2000 times
{
System.Random rnd = new System.Random();
int dice = rnd.Next(1, SplitDenominator + 1); //Roll a dice
print(dice);
if (dice <= splitLevelChance)
{
normal++;
// SceneManager.LoadScene(levelToLoad);
}
else
{
split++;
// SceneManager.LoadScene(levelToSplit);
}
}
print("normal: " + normal);
print("split:" + split);
So this is working fine with stuff like 1/4 or 1/8 but when i do for example 8/ 18 its being inaccurate.
Below is the tests i did with 8/18
Test 1 - normal = 899, split = 1101
Test 2 - normal = 879, split = 1121
Test 3 - normal = 885, split = 1115
If my math is correct, normal should get around 615 and split 1385
Edit: my math was wrong, the code actually works correctly