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

Random without repeating

Discussion in 'Scripting' started by longbottomup, Mar 6, 2015.

  1. longbottomup

    longbottomup

    Joined:
    Nov 2, 2013
    Posts:
    49
    My script:
    void Update(){
    int i = Random.Range(1, 6);
    Debug.Log(i);
    }
    It's always return one way when i restart the scene: "5, 2, 3, 4, etc...."
    Please tell me why? I want it different, how can i do that?
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    It should be different every time unless you did set a seed value. Maybe somewhere else in your code something is setting Random.seed to some value. If not in your own code, possibly in a plugin.
     
    longbottomup likes this.
  4. longbottomup

    longbottomup

    Joined:
    Nov 2, 2013
    Posts:
    49
    Thank you very very much, you save my life.
     
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    ya its rather stupid that seed is a static property of the Random class. if random seeds are needed it really should be a object property, so people can just create a new object from Random with there seed, allowing there to be multiple random objects each with there own seeds.