mardi 28 février 2017

Randomize values without exceeding a value?

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 = startPts - 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.




Aucun commentaire:

Enregistrer un commentaire