// This RNG uses a Linear Congruential Generating method // Where Xn = (Xn-1 * a + c) % m // It is a simple method but it is efficient and fast // These are preset values // You can change them if you want // But i dont recommend it // Here is a list of different values commonly used: // https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use m = 2 ^ 32 // Modulus a = 1664525 // Multiplier c = 1013904223 // Increment // This is the starting seed of each iteration // you can change this value at any time using setSeed() // It is initialized as the current time so that // Each time you compile a new sequence of random numbers is generated time = ($.time() * c + a) % m let old_seed = time setSeed = (s) { old_seed = s } // Implementation of LCG // Generates an integer between 0 and 2^32 - 1 lcgNextInt = () { new_seed = (old_seed * a + c) % m old_seed = new_seed return new_seed } // Generates a decimal between 0 and 1 lcgNextFloat01 = () { return (lcgNextInt() / m) } // Generates a decimal within the given range lcgNextFloatInRange = (min, max) { value = lcgNextFloat01() scale = max - min result = min + value * scale return result } // Generates an integer within the given range lcgNextIntInRange = (min, max) { value = lcgNextInt() scale = (max - min) / (m - 1) let result = (value * scale) + min result = $.floor(result) return result } return { seed: setSeed, randomInt: lcgNextInt, randomFloat: lcgNextFloat01, randomFloatInRange: lcgNextFloatInRange, randomIntInRange: lcgNextIntInRange }