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

How can i make random.Range() generate always even numbers?

Discussion in 'Scripting' started by martis941, Feb 23, 2016.

  1. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    So im trying to make a little minigame where you get random numbers and have to guess the answer , But its supposed to help youngers to learn math . So i wont let the program generate number such as 7/3 because they wont know how to answer , i want the digits to be always even. How can i do it?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    half the range and times by 2?
     
  3. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    this is random digit \/
    digit2 = Random.Range(1, 5); // what you mean?
     
  4. dani-unity-dev

    dani-unity-dev

    Joined:
    Feb 22, 2015
    Posts:
    174
    Code (CSharp):
    1. int max = 1234;
    2. int r = Random.Range(0, max);
    3. if (r % 2 != 0)
    4.     r += 1;
     
    malakadam likes this.
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    random numbers between 1 and 10 => Random.Range(1, 11)
    random even numbers between 1 and 10 => Random.Range(1, 6) * 2
     
  6. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    Thanks , if you have time , i have one little question , for now ive something like this and i want the signs to be randomly generated i mean ( + , - , * , / ) they are randomly choosen and they affect the answer . I dont know how to make them random and how to tell the script to show the answer depending on the sign that was generated
     

    Attached Files:

    • 1.png
      1.png
      File size:
      295.2 KB
      Views:
      882
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  8. martis941

    martis941

    Joined:
    Feb 22, 2016
    Posts:
    95
    How would it look like then? i never used an enum im noob :/
    Code (CSharp):
    1. Enum mySigns{
    2. " + " =1
    3. " - "=2
    4. " * "=3
    5. " / "=4
    6. mySigns choice = GetRandomEnum<MySignsEnum>();
    7. }
    i dont know how should i give them a value to make it work like i want :C