Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by joostjordens · May 05, 2020 at 10:49 AM · randomscripting beginnerseed

Question regarding getting consistent values from Random.value.

I want to control the output of every random value with a seed. When I code in Houdini's VEX language I use rand(seed) to get a value between 0-1 consistently. Here in Unity C# I understood that with Random.value I also get a value between 0-1 and with Random.InitState() I set the seed. When I run Random.value again it results in a different value. What changed the seed?

     public float result1;
     public float result2;
 
     void Start()
     {
         Random.InitState(1);
         result1 = Random.value; // 0.9996847
         result2 = Random.value; // 0.7742628 - Why is this different? What is the seed?
         Debug.Log("Result 1: " + result1);
         Debug.Log("Result 2: " + result2);
     }

Why do I have to set Random.InitState(1); again like this?

     public float result1;
     public float result2;
 
     void Start()
     {
         Random.InitState(1);
         result1 = Random.value; // 0.9996847
         Random.InitState(1);
         result2 = Random.value; // 0.9996847
         Debug.Log("Result 1: " + result1);
         Debug.Log("Result 2: " + result2);
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · May 05, 2020 at 12:16 PM

A pseudo random number generator is an algorithm which produces a pseudo random number sequence. It will always produce the exact same sequence. However you can seed such a generator to start at a "certain" point in that sequence. When you call InitState you do exactly that. You initialize the internal state of the PRNG based on the given seed value.


Your confusion might come from the fact that Random.value is not a field / variable but it's a property. So reading the value property will advance the PRNG to get the next number in the sequence. It should be pretty intuitive that reading "Random.value" to give you a "random value"


Note that a seed value works just like a real (plant) seed. It just sets the initial state and the system then grows based on that initial seed.


Unity's Random class "currently" uses the XorShift 128 algorithm. How Unity actually uses the provided seed to actually "seed" the PRNG I don't know. However the state of the xorshift must not be all 0 since it relies solely on permutation of the internal state. Since the seed you can provide is just a single 32 bit integer when you call InitState they might simply use your provided value as one of the 4 internal state integers and set the others to fix predefined values.


The exact implementation Unity uses we don't know since it's implemented inside the native part of the Unity engine. Unity already changed it's algorithm once in the past. (AFAIK they used a Mersenne twister in the past and then switched to xorshift128). If you want to ensure to get the same behaviour, even in 10 years from now, you might want to use your own implementation. I've once implemented the xorshift 64 variant. Of course larger internal states "usually" result in higher quality random numbers and longer periods. Every PRNG has a certain period. After that amount of numbers the numbers just repeat. This period can't be larger than the numbers of bits in the internal state (but can be way lower depending on the algorithm). According to the wiki article the xorshift128 does actually have the highest period of 2^128-1 which is about 3.4 * 10^38 numbers. Just in case you can't read scientific notation that's a 3 followed by 38 digits. So in any real life applications they will never repeat ^^.


Note that when you use a specific seed for every number you want to generate, that's not "random" at all. It's just one step in a fix computation. Just like if you multiply the value by 42 for example. So given the value 1 your get 42 as a result. For a value of 2 you get a value of 84 and so on. That's exactly what would happen when you set the state to a fix seed and do a single iteration.


So I'm not sure what you want to do here. Using a fix seed every time you query a number doesn't seem to make much sense.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joostjordens · May 05, 2020 at 12:47 PM 0
Share

Understood. Thinking of the seed as an initial starting point in a sequence makes a lot of sense. I was not looking for any result specific. This question was purely for me to understand how seeds work in Unity. Thank you for the detailed response @Bunny83!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

139 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to call a user input variable in another script. 1 Answer

How do I generate a "random" number from a seed? 0 Answers

How to add different random-class objects to different gameobjects? 1 Answer

Can't access string in another script 2 Answers

Recording and playback of gameplay using events (NOT video) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges