Randomize values without exceeding a value?

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 had thought in this

public int startPts = 10, usedPts = 0;
public int skill1 = 0, skill2 = 0, skill3 = 0, skill4 = 0;

public void ButtonRandom(){

 startPts = 10;
 usedPts = 0;

 skill1 = Random.Range( 1, 10 );
 usedPts += skill1;

 skill2 = Random.Range( 1, usedPts );
 usedPts += skill2;

 skill3 = Random.Range( 1, usedPts );
 usedPts += skill3;

 skill4 = Random.Range( 1, usedPts );
 usedPts += skill4;

 startPts = startPts - usedPts;

}

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.

Thank you, guys.

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!

From quickly looking at your code, it appears you don’t specify the correct range in your random function.

public int startPts = 10, usedPts = 0;
public int skill1 = 0, skill2 = 0, skill3 = 0, skill4 = 0;
 
 public void ButtonRandom(){
    startPts = 10;
    usedPts = 0;
    skill1 = Random.Range( 1, startPts );
    usedPts += skill1;
    skill2 = Random.Range( 1, startPts - usedPts );
    usedPts += skill2;
    skill3 = Random.Range( 1, startPts - usedPts );
    usedPts += skill3;
    skill4 = startPts - usedPts;
    usedPts += skill4;
    startPts = startPts - usedPts;
 }

I did not test it but with this you should not go over your start points, and use all of the remaining points in the fourth skill.

I did it this way, in case someone else has the doubt. :smiley:

public int startPts = 10, usedPts = 0;
public int skill1 = 0, skill2 = 0, skill3 = 0, skill4 = 0;

public void ButtonRandom(){
   startPts = 10;
   usedPts = 0;

   int[] skills = new int[4];

   for (int i = 0; i < 4; i++){
        skills *= 1;*

}

for (int i = 0; i < (startPts - skills.Length); i++) {
int tempRandom = Random.Range (0, 4);
skills [tempRandom]++;
}

for (int i = 0; i < 4; i++){
usedPts += skills*;*
}

startPts = startPts - usedPts;

skill1 = skills[0];

skill2 = skills[1];

skill3 = skills[2];

skill4 = skills[3];

}