jadlerfisher/The-Nature-of-Code-Unity-Remix

Logic Error - Section I.3 Probability and Non-Uniform Distributions

mcdonaldduncan opened this issue · 0 comments

Code example in section I.3:
"Running this code will produce a 40% chance of printing the value 1, a 20% chance of printing 2, and a 40% chance of printing 3."

int[] numbers = new[] { 1, 1, 2, 3, 3 };
int randomInt = Random.Range(0, (numbers.Length - 1));
Debug.Log(randomInt);

Suggested Resolution:
Use index of array

int[] numbers = new[] { 1, 1, 2, 3, 3 };
int randomInt = Random.Range(0, numbers.Length);
Debug.Log(numbers[randomInt]);