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 /
avatar image
0
Question by mossbeard1 · Aug 22, 2018 at 08:25 PM · random.rangegameplayrandomizationrandom genminigame

Is there a better way for randomization for triggering a percentage of the time

Hello everyone so currently I am choosing random game objects to trigger think of them like different mini-games. I want them to each have a certain percentage to activate to keep stuff active such as 25,25,20,20,10 and I have written the code below and it does the job but not very well just want to see what others suggest! even if its something as simple as making random.range more random all answers are helpful!!

 void SwapMachine()
     {
         if (lastMono != null && lastMono.doneTurn())
         {
             int index = Random.Range(0, 100);
             Debug.Log(index);
             if (index <= 25)
             {
                 Right = 0;
                 Up = 0;
                 Down = 0;
                 Slot = 0;
 
                 TriggerMiniGame(gameEvents[0]);
                 Left++;       
                // if(Left > 3)
                 Reset(Left, 0);
 
 
             }
             else if (index > 25 && index <= 50)
             {
                 Left = 0;
                 Up = 0;
                 Down = 0;
                 Slot = 0;
 
                 TriggerMiniGame(gameEvents[1]);
                 Right++;
                // if(Right > 3)
                 Reset(Right, 1);
 
 
             }
             else if (index > 50 && index <= 70)
             {
                 Left = 0;
                 Right = 0;
                 Down = 0;
                 Slot = 0;
 
                 TriggerMiniGame(gameEvents[2]);
                 Up++;
                 //if (Up > 3)
                 Reset(Up, 2);
 
             }
             else if (index > 70 && index <= 90)
             {
                 Left = 0;
                 Right = 0;
                 Up = 0;
                 Slot = 0;
 
                 TriggerMiniGame(gameEvents[3]);
                 Down++;
                 //if (Down > 3)
                 Reset(Down, 3);
 
             }
             else if ( index > 90)
             {
                 Left = 0;
                 Right = 0;
                 Up = 0;
                 Down = 0;
 
                 TriggerMiniGame(gameEvents[4]);
                 Slot++;
 
               //  if(Slot > 3)
                 Reset(Slot, 4);
 
             }
             else
             {
                 Debug.Log("should not trigger, something is wrong");
             }
 
         }
     }


Comment
Add comment · Show 3
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 JVene · Aug 22, 2018 at 09:21 PM 0
Share

While I see a few things to suggest in an answer, we need a little more information. Having your code is excellent, but what we need to better understand is:


What is meant by "keep stuff active such as 25,25,20,20,10". I don't have sufficient context to understand what this means, or what this sequence of numbers represents.


This phrase, "it does the job but not very well". From what I see in the code, I can infer the job it is doing, but I can't divine what "not very well" actually means. There is some difference between what it is doing and what you prefer, but I need the meaning of that fleshed out in order to select among a dozen ideas I could suggest.

avatar image breban1 · Aug 22, 2018 at 09:40 PM 0
Share

Your "greater than" checks are unnecessary because the "if" statements before them takes care of those indexes. If you make those changes, your code will be easier to read and less likely to have bugs in the future if you ever decide to change the percentages. Other than that, it looks good!

Example:


    if (index <= 25)
    {
         // same code as before
         ...
    }
    else if (index <= 50)
    {
         ...
    }

etc.

avatar image Casiell · Aug 22, 2018 at 09:45 PM 0
Share

Define "not very well". This code should do exactly what you described.

If you want more randomization you could consider modifying your code to store your last object and exclude it from next draw

1 Reply

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

Answer by SunnyChow · Aug 23, 2018 at 06:59 AM

When i do weighted randomization, i like to use to group up the weight and feedback as an object, sum up the weight, and do a randomization in a loop. it's easier for me to add item or change weight.

 class Item{
 public float weight;
 public System.Action onTrigger;
 }
 
 List<Item> list = new List<Item>();
 list.Add(new Item(){weight=5,onTrigger=delegrate{
 DoThis();
 }});
 list.Add(new Item(){weight=5,onTrigger=delegrate{
 DoThat();
 }});
 
 float weightRnd = 0;
 foreach(Item item in list){
 weightRnd+=item.weight;
 }
 weightRnd*=Random.value;
 
 foreach(Item item in list){
 if(weightRnd<item.weight){
 item.onTrigger();
 break;
 }else{
 weightRnd -=item.weight
 }
 }
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 mossbeard1 · Aug 23, 2018 at 07:54 PM 0
Share

Not gonna lie I don't really understand the code with the weight and everything but I believe this is what I'm looking for or at least in the ballpark!

avatar image SunnyChow mossbeard1 · Aug 24, 2018 at 02:44 AM 0
Share

your "25,25,20,20,10" numbers are the weights. okay! i just found an article about this metohd. (https://medium.com/@peterkellyonline/weighted-random-selection-3ff222917eb6)

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

92 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 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

Is it possible to always make animations play differently? 0 Answers

Random Generation Algorithm Error 3 Answers

How to make random number generation more random? 2 Answers

trying to add rarity to items, don't know whats wrong with this script 1 Answer

Random.Range is not changing? 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