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
1
Question by NinjaSquirrel · Dec 18, 2011 at 09:41 PM · randomfloatseed

How do I generate persistant float values from a random seed?

I have a random seed. I want to generate a float between 0 and 1 from that seed. How do I do this in unity to make sure I get persistent results when using the same seed again, yet different seeds return completely different results, like you would expect?

I believe it has something to do with bitwise operators or something? I am not very familiar with this, can you help shed some light on this for me?

Thanks.

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

3 Replies

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

Answer by Bunny83 · Dec 18, 2011 at 11:19 PM

I've just tested Unity's pseudo-random-generator and it works as expected. Just set your integer seed with Random.seed and then use Random.value to get your float values. Keep in mind that any call to Random.Range or any query of Random.rotation, Random.onUnitSphere, Random.insideUnitCircle, Random.insideUnitSphere, Random.value will continue the pseudo number chain.

Also keep in mind that like in all compiler / language descriptions mentioned they could change the random generator implementation at any time so it could be possible that in future relases it will produce a different set of numbers.

If it's very important that the sequence stays the same you have to implement your own pseudo number generator. One of the simplest is a linear congruential generator. Since i grown up with Pascal / Delphi i'm used to borlands implementation ;)

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 Eric5h5 · Dec 18, 2011 at 11:29 PM 0
Share

they could change the random generator implementation at any time

In fact they have, from Unity 2.x -> 3.0. And yes, it prevented me from updating one of my projects. Lesson learned....

avatar image Bunny83 · Dec 18, 2011 at 11:37 PM 0
Share

:) It would be nice if they could publish what generator they use / used so you could replicate it yourself.

avatar image Eric5h5 · Dec 18, 2011 at 11:51 PM 0
Share

Yep, I had that thought.

avatar image
2

Answer by Eric5h5 · Dec 18, 2011 at 10:55 PM

 Random.seed = 1;
 print (Random.value);
 print (Random.value);
 Random.seed = 2;
 print (Random.value);
 print (Random.value);
 Random.seed = 1;
 print (Random.value);
 print (Random.value);
Comment
Add comment · Show 2 · 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 NinjaSquirrel · Dec 18, 2011 at 11:19 PM 0
Share

In my situation the "seed" is "seed = Random.seed". How do I convert a long seed into a number between 0-1?

avatar image Bunny83 · Dec 18, 2011 at 11:24 PM 0
Share

Nearly all pseudo number generators use either a 32 or 64 bit seed in the background. It will be changed each pseudo random step and stored in the background. So a seed is usually always 32 or 64 bit long (depends on implementation). A integer pseudo random number can be easy converted into a float by dividing it by "max int".

avatar image
0

Answer by MountDoomTeam · Jan 18, 2013 at 09:44 PM

Interesting. i used a modulo system for pseudorandom numbers previously.

 seed = 1;
 random number 1 = (seed * 314.15144)%1
 random number 2 = (seed * 223.60613)%1
 etc up to random number 20

etc. it's not that random because it's quite periodic. but it's a good start for coding. also you can slowly change the seed and the random numbers will slightly vary, so creating controllable mods of a setting that you like. that said I want something similar and yet better.

I always want a random seed that can generate 10 20 random numbers for complex presets on things, terrain, patterns, etc, so i guess you could set Random.seed and then generate 20 rand values and set them as variables. Although if you get a nice setting and you want to find mods of it, Random.seed is Integer and you can't generate similar values as with perlin noise etc.

perhaps perlin noise in unity is largely fine for controllable rand nums also.

Comment
Add comment · 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

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

8 People are following this question.

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

Related Questions

Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS? 1 Answer

How to save a random seed to a playerpref 2 Answers

How to generate a Random Float Variable 4 Answers

Randomized Gun Spread 1 Answer

Set Random.seed for noise generator 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