Search Unity

Random.randomBoolean function!

Discussion in 'Wish List' started by cupsster, Mar 25, 2011.

  1. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    Code (csharp):
    1. function randomBoolean ():boolean
    2. {
    3.     if (Random.value >= 0.5)
    4.     {
    5.         return true;
    6.     }
    7.     return false;
    8. }
    Return : boolean [true or false] cmon guys! Why it's not already there?.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,123
    Well, you wrote it, so now you have it. Problem solved. Usually the wish list is for things that are hard/impossible to add yourself.

    --Eric
     
  3. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,625
    This is hardly a commonly requested feature nor difficult to program?
     
  4. HonorableDaniel

    HonorableDaniel

    Joined:
    Feb 28, 2007
    Posts:
    2,801
    Even shorter. :)

    return (Random.value > 0.5f);
     
    nguyenlamlll and SaSha_K like this.
  5. MorphVGX

    MorphVGX

    Joined:
    Jun 21, 2010
    Posts:
    57
    It think this will perform better:
    return (Random.Range(0, 2) == 1);
     
    SaSha_K and McGravity like this.
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,123
    It performs somewhat worse than Random.value > .5, actually.

    --Eric