Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved A question about Random.InitState

Discussion in 'Scripting' started by Elvin-Hillbird, Nov 13, 2023.

  1. Elvin-Hillbird

    Elvin-Hillbird

    Joined:
    Nov 15, 2012
    Posts:
    13
    Hi,
    I use Random.Initstate to set a seed to create a deterministic pirate treasure map. But after that I need to randomize the seed again. Because if I don't, the game throws the same dices for the turn based combats etc.

    I am using Random.InitState((int)DateTime.Now.Ticks) to set the random seed. But this number is so large like 10 digits. My question is this: Since this number is so big can it slow down the calculations? Should a smaller number like DateTime.Now.Seconds can be better? (But that one comes up 20 or 40 sometimes which is so small I guess) Am I overthinking? Thank you for your help.
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    771
    Different types of numbers like byte, int, long, double etc can perform differently but the size of the value in each type doesn't have any noticeable impact on performance. The random function won't perform any differently if your seed is 1 or 10000000.



    I nearly forgot... ArrrRRrrrr...
     
    Bunny83 and Elvin-Hillbird like this.
  3. Elvin-Hillbird

    Elvin-Hillbird

    Joined:
    Nov 15, 2012
    Posts:
    13
    Thank you for the answer!
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,375
    You can also always user the Profiler or use the
    System.Diagnostics.Stopwatch
    class to test this on your end as well. And depending how often you using Unity's
    Random
    class, it might not matter at all. Ergo, if you're only calling it every so often for a dice roll, the performance impacts are completely negligible.

    It also might be worth encapsulating your randomiser in a class and using an instance of either Microsoft's
    System.Random
    , or the implementation in the Unity mathematics package: https://docs.unity3d.com/Packages/com.unity.mathematics@1.3/api/Unity.Mathematics.Random.html

    The latter is serialisable so it would be useful for saving the state of it to enable save-games that don't allow save-scumming (unless you want allow that).
     
  5. Elvin-Hillbird

    Elvin-Hillbird

    Joined:
    Nov 15, 2012
    Posts:
    13
    Some times game restocks shop items and calls random.range maybe 70-80 times in a frame. I used profiler and didn't notice any difference if I use a long seed or a short seed. No effect on frame rate too.
     
    Last edited: Nov 13, 2023
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    607
    Unity's and Microsoft's Random classes are very fast, because they're not real random numbers generators (RNG). They're basically just like a lookup table, simulating being a RNG. So don't worry about performance if you're using them.
     
    Elvin-Hillbird likes this.