Hi guys, I have some time thinking the following, I want to make a button to randomize whole values of some skills. The question is that I have 10 points to distribute between 4 skills, the idea is to have selected randoms numbers without exceeding 10 points.
I also try with several conditionals and repetitive methods, but I do not get the desired result. Since sometimes it surpasses the 10 points, leaves points without using or only changes the first 2 values when I put the conditions.
I did something similar to this in my current project, but it used floats instead of whole numbers. Basically what you need is to factorize them numbers (I think that’s what its called idrk), something where you generate all numbers randomly, and then adjust the total to be 10. It’s easy with float, not as easy with ints!
For floats, it looks like…
sum = num1 + num2 + num3 + num4;
factor = 10 / sum;
num1 *= fac;
//so on
Again, this works perfect for floats, the new sum would = 10. You can apply this principle and get your stats as floats, and then just round. It wont always be 10, but you could just add 1 to a random spot.
Another method if you want random ints, would be to just pick a random stat and add 1 to it, 10 times!
int[] skills = new int[4];
int max = 10;
int sum = 0;
while (sum < 10)
{
if (skills[Random.Range(0,4)] >= max)
continue;
skills[Random.Range(0,4)]++;
sum++;
}
skill1 = skills[0];
//So on...
I would recommend keeping your skills as an array, it makes it a lot easier to loop through them, and keeps your code cleaner!