Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nlbi21 · Mar 01, 2017 at 07:16 AM · randomrandom.rangerandomization

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rob2309 · Mar 01, 2017 at 02:58 PM 0
Share

I think you should use Random.Range(1, 10 - usedPts), since this gives you a number between one and the points you have left, ins$$anonymous$$d of the points you have used..

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by oStaiko · Mar 01, 2017 at 06:27 PM

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nlbi21 · Mar 02, 2017 at 07:58 AM 0
Share

This is very similar to the desired result, thank you man.

avatar image Titus2727 · Apr 12, 2017 at 12:55 AM 0
Share

This Helps me a lot with my game too! But is there a way i can do this while all of my skills start at a base value of one?

avatar image oStaiko Titus2727 · Apr 21, 2017 at 06:41 AM 0
Share

If its the first method, just make the sum smaller than the actual sum by the number of stats, complete it as normal, and then add 1 to the end results. Ex. Set sum to 15 if you have 5 stats and want actual sum to be 20, then add 1 to each of the 5 to reach the 20.

For the second method, just add 1 to each of the stats before, and then keep the sum the same.

avatar image
0

Answer by eldruz · Mar 01, 2017 at 02:56 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Mar 01, 2017 at 05:52 PM 0
Share

I think this still needs a bit more work to produce desired results.

Random.Range with integers never produces the latter argument (only values smaller than it) and that should be taken into account.

With this code the highest value a skill can get is 9 for Skill1, 8 for Skill2, 7 for skill3 etc. The values could for example be put into a temporary list and picked from there from a random index for each skill so each skill gets an equal chance of getting the highest possible value.

Also if skill1 gets 9 as its value, there's not enough points to give 1 point for each of the remaining skills. So startPts should probably be reduced by the number of skills to begin with.

avatar image eldruz NoseKills · Mar 02, 2017 at 07:50 AM 0
Share

The issues you mention are definitely spot on, there's a lot that could be done to the repartition of the values on the different skills. These are definitely areas op needs to ponder.

Lacking more information about the intent of op I opted to just correct the matter at hand.

avatar image
0

Answer by nlbi21 · Mar 04, 2017 at 09:09 AM

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

 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[i] = 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[i];
     }
 
    startPts = startPts - usedPts;
 
    skill1 = skills[0];
 
    skill2 = skills[1];
 
    skill3 = skills[2];
 
    skill4 = skills[3];
 
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kosmosaik · Dec 11, 2018 at 12:18 PM 0
Share

Hi!

A bit late to the party, but I'm writing a loot system in this moment in Unity. Right now I have a button that instantiate a random item with random stats. I have a similar function built like yours.

The thing is that every iteration in the loop, it's adding one point to a random index in the array, right? The problem I have with this is that it's not really randomizing the stats. It's still based on probability. Like if you roll a dice 1000 times, the result for how many times you rolled 1-6 is going to be pretty much the same on each value, leaving my items with similar values on all attributes. Is this loop doing the same? Let's say you have 1000 points to distribute to all of your skills, wouldn't it distribute them pretty equally (plus-$$anonymous$$us a few values)?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make random number generation more random? 2 Answers

Random.Range is not changing? 1 Answer

Random.Range(..) not working 1 Answer

Random objects in coordinates specific 1 Answer

I made this script to instantiate objects, but when the game starts I get this error:IndexOutOfRangeException: Array index is out of range. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges