Doing a random range from 0 to length of the array, is saying that each entry has the same probability of being selected.
An example:
world1 - Probability 1
world2 - Probability 1
world3 - Probability 1
So with a total of 3 probability, they each have a 1 in 3 chance of being picked.
What you want is something more like this:
world1 - Probability 10
world2 - Probability 5
world3 - Probability 1
With a total probability of 16, the first world has a 10 in 16 change of being pick, the second is 5 in 16 and third is 1 in 16.
To accomplish this in code, you’ll need to track the world’s probability, so as with another array with matching indexes. You’d add up the probabilities then do a random range from using that total. Then check if that number lands within a probability.
string[] worlds;
int[] prob;
bool[] chosen;
private void Start()
{
worlds = new string[] { "thisworld", "thatworld", "someworld", "otherworld" };
prob = new int[] { 10, 10, 5, 1 };
chosen = new bool[worlds.Length];
}
public void changeWorld()
{
int totalvalue = 0;
int runningvalue = 0;
// get sum of probability of world that can be picked
for (int i = 0; i < worlds.Length; i++) {
// if not already chosen before
if (!chosen*) {*
_ totalvalue += prob*;_
_ }_
_ }*_
* // choose a random number with the total*
* int hitvalue = Random.Range(1, totalvalue + 1); // adjust for exclusive max with ints*
* // run through checking for hits*
* for (int i = 0; i < worlds.Length; i++) {*
* // if not already chosen before*
_ if (!chosen*) {
runningvalue += prob;
if (hitvalue < runningvalue) {
// probability hit*
currentWorld = worlds*;
chosen = true;
}
}
}*_
* }*
Let me know if you have issues with this or have further questions on this topic.