|
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:
(comments are locked)
|
|
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.
Sep 06 '11 at 08:42 AM
Sir Keyzinburga
It looks complicated, but the Lehmer RNG is essentially one line of code- and it's even included in the page.
Sep 06 '11 at 09:03 AM
boat365
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 instead.. function lcg_rand(a:float):float { seed = (a * 279470273f) % 4294967291f; Debug.Log("seed: " + seed); return (seed/4294967291.0f); }
Sep 06 '11 at 10:14 AM
Sir Keyzinburga
I don't know about representing numbers in Unityscript; I use C# exclusively. Here's how it looks there: class LCGRand 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
Sep 06 '11 at 10:41 AM
boat365
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.
Sep 06 '11 at 02:25 PM
Sir Keyzinburga
(comments are locked)
|
