- Home /
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.
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 ;)
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....
:) It would be nice if they could publish what generator they use / used so you could replicate it yourself.
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);
In my situation the "seed" is "seed = Random.seed". How do I convert a long seed into a number between 0-1?
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".
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.
Your answer
Follow this Question
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