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

Strange behaviour when setting Random.seed

Discussion in 'Scripting' started by Iune, Feb 19, 2015.

  1. Iune

    Iune

    Joined:
    Feb 19, 2015
    Posts:
    2
    Hello,
    in the code below 20 random numbers are first generated. After generating 10th number the seed is saved and rest of the numbers are generated as usual. After that the saved seed is set to Random.seed and another set of 10 random numbers are generated. If the line "Random.seed = seed" is not used the results are different than the last 10 numbers. Can you explain why this is happening?

    Code (csharp):
    1.  
    2.     Random.seed = 1234;
    3.      int seed = 0;
    4.    
    5.      Debug.Log("First set of 20 random numbers, after 10th number save the seed.");
    6.      for (int i = 0; i < 20; i++)
    7.      {
    8.        int r = Random.Range(0, 50);
    9.  
    10.        Debug.Log((i == 10 ? " -> " : "") + r + " - " + Random.seed);
    11.        if (i == 10)
    12.        {
    13.          seed = Random.seed;
    14.          Random.seed = seed; // If this line is not used code gives different numbers in the last for-loop.
    15.        }
    16.      }
    17.    
    18.      Random.seed = seed;
    19.  
    20.      Debug.Log("Another set of 10 random numbers. Seed: "+Random.seed);
    21.      for (int i = 0; i < 10; i++)
    22.      {
    23.        int r = Random.Range(0, 50);
    24.  
    25.        Debug.Log(r + " - " + Random.seed);
    26.  
    27.      }
    28.  
     
  2. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    The behaviour is correct.
    If you don't use line 14, then both the second half of the first for loop and the last
    for loop have different seeds and this is why you will get different set of numbers.

    But when you use line 14, then they both get the same seed value and so the
    set of numbers are same.
     
  3. Iune

    Iune

    Joined:
    Feb 19, 2015
    Posts:
    2

    I might be confused, but isn't Random.Range using the seed _currently_ in the Random.seed? To me I should get the same exact behaviour with or without the line 14. What is the use of reading Random.seed if it changes every time I use Random.Range and it doesn't mean I can use the current seed to come back to that position in the random number series? It confuses me why I can get two different numbers with the same seed (when not using line 14).
     
  4. SpectreCular

    SpectreCular

    Joined:
    Jan 15, 2015
    Posts:
    44
  5. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    aah ok I got the problem now..Indeed its strange. If you seed the sequence with a
    particular value(the value you store at i == 10), then it should give the same
    numbers without line 14 as well..but before I point finger at Unity, I'm going to do a few more tests because this is really interesting :)
     
  6. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
  7. gamer_boy_81

    gamer_boy_81

    Joined:
    Jun 13, 2014
    Posts:
    169
    Ok, I made quite a lot of tests and it looks like a bug in the seeding behaviour.
    Line 14 should not be required.