Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Percent chance of calling an int.

Discussion in 'Scripting' started by batistaa65, Oct 20, 2017.

  1. batistaa65

    batistaa65

    Joined:
    Jul 17, 2014
    Posts:
    56
    I'm on the phone right now, but I just thought of something... If I had a string array of say types of fish eg. Carp, catfish, etc. If I wanted to call one of the strings, say carp is element 0, could I just do a random.Range of say 1-100, and if it falls on say 40, could I just do,

    Code (CSharp):
    1.  
    2. Public string[] fish;
    3. Public Text carp;
    4. Public int rand;
    5.  
    6. //Carp is element 0.
    7. //Catfish is element 1.
    8.  
    9. If (rand <= 40){
    10. //This is just abstract for the //purpose of this post
    11.    
    12.     carp.text = fish[0].toString();
    13.     //This would come up as the    
    14.    //fish you caught
    15. }
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Yes, but you only need a random range of 10 since your numbers are divisible by 10. If it's 4 or less, would achieve the same thing. Of course your answer is perfectly correct.
     
  3. batistaa65

    batistaa65

    Joined:
    Jul 17, 2014
    Posts:
    56
    I got mixed up from 2 posts I did but I thought of something that someone said on the other, if I use a float 0-1, would that use less resources? Instead of 1-10?
     
  4. batistaa65

    batistaa65

    Joined:
    Jul 17, 2014
    Posts:
    56
    Okay so how would I code it to I guess Cascade into the other percentages?
    Code (CSharp):
    1. If (int <= 4){
    2.     If (int <= 6){
    3.         If (int <= 10){
    4.         Element 2
    5.         }
    6.     Element 1
    7.     }
    8. Element 0
    9. }
    Or would I go the other way?
    10,6,4
     
  5. batistaa65

    batistaa65

    Joined:
    Jul 17, 2014
    Posts:
    56
    Or would I use else statements?
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think greater than might be better;
    if(int>4) choose 60percent;
    else if(int>1)choose 30percent;
    else choose 10 percent;


    It's pseudo code so I didn't use tags. I'm sure it works both ways, just use else if so they don't conflict. Thats figuring your random range is 1 -10. I don't know if I got your percentages right, but 60 percent chance should have 6 correct answers in the range, etc.
     
    Last edited: Oct 20, 2017
  7. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    This is a level of optimization I'm not sure you need. Profile first to make sure this is something worth worrying about (I very much doubt it's worth it).

    Just focus on making it readable. Try starting from the lowest value.

    Code (csharp):
    1.  
    2. if (i < 10) DoSomething();
    3. else if (i < 20) DoSomethingElse();
    4. else if (i < 30) etc...
    5.  
     
    ghostmode likes this.
  8. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819

    Maybe it's more readable, but if you catch the higher percentage first, it's going to probably go faster because it's skipping the other clauses more often. I'm sure for practical purposes, it doesn't matter.