Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Good "deteriministic" RNG algorithm?

Discussion in 'Scripting' started by bloomingdedalus, Feb 16, 2015.

  1. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    I'm trying to find something that will allow me to initialize it with a seed, and for every set of 3-space coordinates I pass it, it will output a random integer - the integer needs to always be the same for each coordinate set and should vary greatly according to the seed. I've played around with a few algorithms that work ok - mostly of my own creation, but I lack the statistics knowledge to know how "random" my "muss it all up - but logically" algorithms are.

    Is there a well known creme de la creme method for doing this?
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    If I understand you correctly you're wanting a unique random integer for each vector3 coordinate, and that random number should always be the same for that particular vector3 coordinate. The standard way that's done is as follows:
    Code (JavaScript):
    1. Random.seed = x * 100 + y * 50 + z * 10;
    The number each axis is multiplied by can be changed to whatever you want and so long as you always multiply by the same numbers you'll end up with a unique random integer for every unique vector3 coordinate.

    I too have tried a lot of algorithms I've created myself, some of them pretty complex, and none have ever given me results as good as this simple formula.
     
  3. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    I was trying to avoid reinitializing a "Random" class each time... Although I haven't tested the speed at which Random is initialized. It's for a 3-d Perlin algorithm, so there's going to be a lot of coordinates and it needs to be as fast as possible. I may do some Diagnostics.Stopwatch tries on your method.

    Here's the source code for System.Random() in .NET - as you can see the initialization sequence is a whole bunch of words: http://www.dotnetframework.org/defa...lr/src/BCL/System/Random@cs/1305376/Random@cs