Search Unity

  1. Tune in to get all the latest Unity news live from Berlin on June 19, 6pm CEST. Set up a YouTube reminder!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

I want some help with having some Randome Values

Discussion in 'Scripting' started by Lotfi13, Jun 14, 2018 at 8:08 PM.

  1. Lotfi13

    Lotfi13

    Joined:
    Thursday
    Posts:
    2
    Hello everybody
    I want to know how we can have a random value between known values
    ex:
    I want a random value between (1,3,8,6)
    and I do not want him to return any other value than these four
    so Random.Range is not valid
    and thank you
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,672
    Add the numbers to a list/array.
    Then do a random.range to pick an index from that list or array
    Return the value at that index.
     
    Doug_B and whileBreak like this.
  3. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    272
    Code (CSharp):
    1. int[] numbers = new int[4]{1,3,8,6};
    2.  
    3.  
    4. ...
    5. int rndNumber = numbers[Random.Range(0,4)];
     
  4. Lotfi13

    Lotfi13

    Joined:
    Thursday
    Posts:
    2
    These values change with time
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,672
    You can just redo the list than. Add or remove or start it fresh when you need to.
     
    whileBreak likes this.