Search Unity

Probability Check code?

Discussion in 'Scripting' started by leegod, Jun 24, 2019.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    Code (CSharp):
    1. public bool ProbabilityCheck(float val)
    2.     {
    3.         float a = Random.Range(0, 100f);
    4.         if (a <= val)
    5.             return true;
    6.         return false;
    7.     }
    I wrote like this but I don't like...

    Because, if coming val is 0, it will return true when a == 0, but it should return false because 0 is 0. Never should happened.

    If doing like [ if (a < val ) ], then when val is 100 and a is 100, it will return false, even if it should return true.

    So then? Of course I can do like if(val == 0) return false;

    But no other way?
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    687
    I'm not 100% sure what the issue is, if you're trying to check val against a just use ==, Random.Range is inclusive, meaning both the lower and upper are included in the random.
     
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    Code (CSharp):
    1. public bool ProbabilityCheck(float val)
    2.     {
    3.         if (val == 0)
    4.             return false;
    5.         float a = Random.Range(0, 100f);
    6.         if (a <= val)
    7.             return true;
    8.         return false;
    9.     }
    So I did revise like above, but don't like this code. No neat way?
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I think we need more context about what you want this to do. The script is obviously doing what it is told, but saying that 0 should return false means nothing to someone not involved in your project.

    As a small tip, you can simply use
    Code (CSharp):
    1. return (a <= val);
    if all you wanted to do is return the bool.

    As far as excluding 0 from the comparisons, yes using val== 0 as a check and returning from there is probably the best way to go about it, as you killing the function right then and there, allowing the program to continue without executing the rest of the code which is redundant in that context, apparently.
     
  5. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Something like this?
    Code (CSharp):
    1. public bool ProbabilityCheck(float val)
    2. {
    3.     return Random.value < val;
    4. }
    where val is betwen 0f (0% probablility) and 1f (100% probablility)
     
  6. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    Just normal probability check. Like in-game's critical hit occurance, item drop probability,,, etc. Most of them will shows like 15% or 0.02% (in case of super rare item drop)
     
  7. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    So https://docs.unity3d.com/ScriptReference/Random-value.html
    Random.value is 1.0 inclusive, if val is 1 and Random.value is 1, then both become same, then will return false.
     
  8. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    So then the best is this?

    Code (CSharp):
    1. public bool ProbabilityCheck(float val)
    2.     {
    3.         if (val == 0)
    4.             return false;
    5.         return (Random.Range(0, 100f) <= val);
    6.     }
     
  9. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Ah, good to know.. then maybe
    Code (CSharp):
    1. public bool ProbabilityCheck(float val)
    2. {
    3.      return val > 0 && Random.value <= val;
    4. }
    5.  
     
    Antypodish and leegod like this.