Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Sendatsu_Yoshimitsu · Jan 26, 2017 at 11:02 AM · randomscriptingbasicsrandomizationstatistics

Why is my weighted loot table only returning the last index on the list?

I'm trying to build a very basic weight loot drop system, where each item gets a drop probability between 0-infinity and the weights are then compared against each other, such that 3 items with drop rates of (50,50,100) will drop at approximately the same ratios as 3 items with rates of (1,1,2):

 public int GetSpriteIndex(List<SpriteSpawn> listOfSprites)
 {

     int index = 0;
     int total = 0;
     for (int i = 0; i < listOfSprites.Count; i++)
     {
         total += listOfSprites[i].chanceToSpawn;
     }
     int value = (int)Random.value * total;

     for (int i = 0; i < listOfSprites.Count; i++)
     {
         value -= listOfSprites[i].chanceToSpawn;
         if (value <= 0)
         {
             index = i;
         }
     }

     return index;
 }


That looks right to me, and I've seen various versions of this used for years, but for some reason it only ever returns the last index on the given list. Did I mess up something obvious, or this entire approach flawed?

Comment
Add comment
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

1 Reply

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

Answer by doublemax · Jan 26, 2017 at 11:17 AM

Replace this:

 int value = (int)Random.value * total;

with:

 int value = (int)(Random.value * total);

you're converting the float to int before the multiplication and this will always return 0.

On a side note, nothing personal:
It always puzzles me that people don't have the slightest clue about even the simplest debugging techniques. Setting a breakpoint and tracing through the code or in this case a simple debug.Log should have been enough to figure out what was going wrong.

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 Sendatsu_Yoshimitsu · Jan 26, 2017 at 11:24 AM 1
Share

Hmkay, thank you for the patient explanation; my apologies for not spending more time debugging in advance. :)

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

66 People are following this question.

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

Related Questions

Random bitmap generator 0 Answers

Randomly generate blocks on a flat map 0 Answers

How to stop getting duplicate numbers while using random.range 1 Answer

Random Scenes Without Repetition 1 Answer

Spawn the next room by opening a door. 3 Answers


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