Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Using random range, but excluding a value somehow?

Discussion in 'Scripting' started by danmct1995, Aug 28, 2020.

  1. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    I want to have an object that receives a random float value between 4 and -4, but ideally I would like to avoid the numbers 1 and -1. I haven't been able to find anything like this and I thought up a solution, but Unity doesn't allow it. What I've tried so far is:

    Code (CSharp):
    1.     public void LaunchOnClick()
    2.     {
    3.         if (Input.GetKeyDown("space"))
    4.         {
    5.             hasStarted = true;
    6.             GetComponent<Rigidbody2D>().velocity = new Vector2
    7.                [B] [/B][U][B](Random.Range(-4, -1) || Random.Range(1,4)[/B],[/U] Random.Range(PushYLow, PushYHigh));
    8.         }
    9.     }
    So the problem with this is that I am unable to use the "||" operand with a float on the same axis. Does anyone know a different solution? I filled in the variables relevant with numbers and underlined + bolded them so its easier to read.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    || is a boolean operator, it doesn't apply to floats.

    A couple ways to do this:

    • Pick a random number between 1 and 4. Then "flip a coin". If heads, multiply by 1, if tails multiply by -1.
    • Pick a random number n between -3 and +3. Then if n > 0 add 1, if n < 0 subtract 1.
     
    mopthrow and danmct1995 like this.
  3. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    Ahh I think I see, so essentially I want to just choose between 1 and 4 and multiplying by a negative 1 would give me all the negative values as well. Ty!
     
    PraetorBlue likes this.
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    You say you want a random float value, but you are calling the integer version of Random.Range. (It automatically limits itself to integers if you pass in integer arguments.) Do you want to get values like 2.6 or 3.8?

    Do want to avoid all values between 1 and -1, or just the exact values 1 and -1? Are you OK if you get 0.2 or -0.4? How about if you get exactly zero? Note that the odds of a random float being exactly any single number you name are already really small (but not zero).
     
  5. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    I filled in my values with numbers to make it easier to read. The variables themselves that take those places contain float values
     
  6. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    Does something like this work for you?

    Code (csharp):
    1. float GetRandomNumber(float min, float max)
    2. {  
    3.     float value = Random.Range(min, max);
    4.    
    5.     while((int)value == -1 || (int)value == 1)
    6.     {
    7.        value = Random.Range(min, max);
    8.     }
    9.  
    10.     return value;
    11. }
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Casting to int for comparisons is pretty confusing. You are much less likely to make a mistake if you do comparisons using inequalities with no casts.

    Also, generating new random values in a loop until you get one that you like is usually not the ideal option, because it could theoretically take a lot of tries if you get unlucky, and if you make a mistake you might accidentally create an infinite loop. There are situations where I'd do it anyway, but if you can think of another reasonable option, that other option is probably better.

    OP still has not given a clear statement of requirements.
     
  8. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    Given his question he doesn't want a random number that is either -1 or 1, and comparing floats can be troublesome, I thought that would be appropriate.
     
  9. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,494
    Code (CSharp):
    1. float value = (value = Random.Range(-3f, 3f)) < 0 ? value - 1 : value + 1;
    If you don't use the "f" with Random.Range values u will get ints from it
     
  10. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Can you even give a precise description of what your code is doing? Does your comparison return true for the value -0.7? How about the value -1.3? I had to look up the cast rules to be sure (specifically, checking whether it rounds towards zero or towards negative infinity).

    I specifically asked the OP what fractional values he was trying to exclude, and he responded to my post but ignored that question, so there's at least 4 different vaguely-plausible things he could want. If I were to rank those in order of how likely I think it is that the OP wants them, your code implements the fourth-most-likely option.
     
  11. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    How about this as another possible solution to the OP's question as I understand it.

    Code (CSharp):
    1. var values = new int[]{-4, -3, -2, 2, 3, 4};
    2. var chosenValue = values[Random.Range(0, values.length)];
     
    danmct1995 likes this.
  12. danmct1995

    danmct1995

    Joined:
    Apr 21, 2020
    Posts:
    70
    This would seem to also work, but I did want float values instead of ints.