Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Bigproblem01 · Mar 02, 2015 at 01:54 PM · randommultiplevalueframeseed

Multiple random values in one frame

Hi guys,

Does anyone happen to know how exactly this works on the background and if it is ok to do:

 float one;
 float two;
 float three;
 float four;
 float five;
 
     void Update()
     {
         one = Random.value;
         two = Random.value;
         three = Random.value;
         four = Random.value;
         five = Random.value;
     }

I need to random bunch of variables in one single frame. When I do it like above and print them, they all seem to random different values and all seems ok. But I heard that since the Random is depended on the clock, randoming in one frame may produce same results... This doesn't happen in my test but I would still like to verify if it's ok to leave the code like this.

If this is fine, then what's the rumor about randoming multiple times in one frame?

Thanks!

Comment
Add comment · Show 1
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 hexagonius · Mar 02, 2015 at 02:32 PM 0
Share

Depends on how Random was implemented. If your tests show different results than some rumours, forget about them.

I would say it's using the internal system clock or other "don't repeat" algorithms anyway, so the time is always different, even between calls within two lines of code.

1 Reply

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

Answer by steakpinball · Mar 02, 2015 at 02:38 PM

When first requesting a random value, the Random class creates a new random number generator. This generator needs a seed which is used to generate the first random value. If one is not provided, the current time in milliseconds is used. All subsequent values are are used to generate the number after it. This means generating several random numbers in the same frame is perfectly fine.


Unity blog has an excellent post about random numbers. http://blogs.unity3d.com/2015/01/07/a-primer-on-repeatable-random-numbers/

Comment
Add comment · Show 3 · 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 Bigproblem01 · Mar 02, 2015 at 03:12 PM 0
Share

by "all subsequent values" you mean milliseconds right? Isn't there a chance that one = Random.value; and two = Random.value; will happen sooner than a millisecond apart?

I'm reading the blog right now btw, thank you!

avatar image hexagonius · Mar 02, 2015 at 03:18 PM 0
Share

He means, that every new number is also based on the preceding number. $$anonymous$$eaning there's no chance having the same number twice as every number is used for the next one calculated.

avatar image Bigproblem01 · Mar 02, 2015 at 03:30 PM 0
Share

Ah I see, makes sense now. Thanks a lot for the help guys!

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

22 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

Related Questions

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

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

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

How do you get a seed value from UnityEngine.Random.State? 2 Answers

Can't access string in another script 2 Answers


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