# Is it Possible to make your own pseudorandom generator?

Its just more so a Simple Question, but probably a very complicated Answer.

So I already know about `math.random()` and `Random.new()`, while all these work for what you want them to, they are basically pseudorandom generators which means that they get a random number using an algorithm.
(let me know if I made any mistakes saying this)

Which got me wondering if you could make your own Random Generator, without using `math.random()` or `Random.new()`, it seems a bit farfetched in my opinion.

Would It be possible to do this?

It would be hard, but not impossible. After all they are pseudo-random. I believe that the basic idea of a random generator is to take an input, and manipulate it somehow so that at the end, the smallest deviation from one input to another, is drastic.

The simplest example I’ve heard is with exponents. if you have two inputs like 9,998 and 9,999 and square them (99,960,004 and 99,980,001 retrospectively) the difference between the two inputs becomes 20,000 which is a huge amount of deviation. Hopefully if the average deviation is large enough, the pattern of randomness to whatever you apply it on appears.

Now since you need an input to actually feed into your random number generator, you want something that varies as well, as it will help mask the pattern of randomness. I think the best way would be to use the precise time that has passed, and only use milli/nanoseconds portion. hopefully every time you check, the number will have updated so fast that it’s not obvious it had counted up linear pattern, giving off a feeling of randomness by itself.

Although you could just use the time by itself to generate the random number which probably isn’t half bad, you will restrict yourself to a small range of numbers, and after thousands of tests, the numbers have chances of repeating (depending on what kind of number you want to return).

It’s a little possible, if you’re not looking for a “clean” pseudorandom generator, it’s pretty easy to do with a math equation, here’s an example:

``````local INCREMENT = 421332346545.55555
local MULT = 1730268543663424
-- ^^ These aren't anything specific, I just mashed my numpad until a number came out

function rand(seed)
seed = seed or os.clock()
return (seed*MULT+INCREMENT) % 1000000 / 1000000
end

function fromToRand(from, to, seed)
return rand(seed)* (to - from) + from
end

print(rand()) -- totally random
print(rand(457)) -- always 0.5312
print(fromToRand(10,400)) -- totally random from 10 to 400
print(fromToRand(10,20,1235)) -- always 16.75328
``````

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.