- Home /
several Random instances
Hello,
I would very much like to have 2 different Random instances, one which poops out real-random (or the unity approach to that) and one of which I can set the seed to a given Integer, making it poop out the same sequence every game cycle.
What i tried: searching on google ofcourse.. but it qutie hard to formulate a question that is not ambigious.
I tried instantiating a Random object by var rnd = new Random() but this is just a new pointer to the Random instance which unity provides. (probably because it is a singleton or something like that)
I hope this is clear enough.
As for sample code with which I tested the outcome:
private var rand:Random;
function Awake(){
rand = new Random();
rand.seed = 18427;
//Debug.Log either rand.value or Random.value -> both will be the same
}
Answer by boat365 · Sep 06, 2011 at 08:30 AM
You could use the .NET framework System.Random instead; that will let you have distinct instances. It's still kind of a voodoo black box thing though, and you can't read the seed, or reseed it if you want to generate the exact same sequence of numbers again (for instance, if you want to save and reload the state of your random number generator).
Fortunately a linear congruential generator is not hard to implement yourself; it may be perfectly sufficient for throwaway random number generation that doesn't need to be particularly robust. See http://en.wikipedia.org/wiki/Linear_congruential_generator
edit: actually this is the wiki page I was thinking of- http://en.wikipedia.org/wiki/Lehmer_RNG
I don't find myself able to understand it well enough to implement one myself, so I guess I will either have to learn it or find another solution.
It looks complicated, but the Lehmer RNG is essentially one line of code- and it's even included in the page.
Sorry, I have been busy trying to get it to work. But I can't get it to work in javascript, probably because I don't have enough +int stats.
What I can make out of it is the inital seed (a) is a number that is coprime to the modulo (n). I used the values given on the wiki, but I guess the problem lies in uint and ulongs, the syntax on the wiki ####UL is not recognized in javascript it seems. I tried to use floats ins$$anonymous$$d..
function lcg_rand(a:float):float { seed = (a * 279470273f) % 4294967291f; Debug.Log("seed: " + seed); return (seed/4294967291.0f); }
I don't know about representing numbers in Unityscript; I use C# exclusively. Here's how it looks there:
class LCGRand
{
public static UInt32 Next (UInt32 a)
{
return (UInt32)(((UInt64)a * 279470273uL) % 4294967291uL);
}
}
Note that the value returned from this function is the seed that should be passed, unmodified, next time you call it (the first time you call it, you supply an initial seed).
If you wanted a random number between, say, 0 and 9 inclusive, you could use 'retval % 10'. But be sure to store the return value, and pass it unmodified to the next call.
Something like this:
// Print 5 random numbers between 0 and 9 inclusive
uint seed = 31;
for(int i = 0; i < 5; i++)
{
seed = LCGRand.Next(seed);
print(seed % 10);
}
Thank you :D I am now able to get non-random random numbers :D Although I had to use UInt64 for all values otherwise it would return (mostly) negative values after the modulo modification.
Your answer
Follow this Question
Related Questions
Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS? 1 Answer
How do re-randomize Random.seed after setting it? 3 Answers
How to generate a random seed at a 24 hour interval 1 Answer
Can I disable randomization on particles? 1 Answer
How many many Pseudo-random sequences can be gotten from Random using Random.InitState()? 2 Answers