What does the shortest pseudo-random algorithm look like?

1

After seeing some impossibly short solutions in this SE, I began to wonder what code someone could come up with to build a pseudo-random number generator. Now given that I am new to programming, I have absolutely no idea what such generator should look like, or what language to write them in. But that did not stop me from trying some goofy things like writing PRNG, listing primes or prime tests.

So here's the deal, the challenge is to write a pseudo-random number generator that does the following things:

1.Accept a seed in the form of string, number, bits, or whatever, and generate a pseudo-random sequence based on that string. The initial seed should default to 0 if nothing is provided.

2.Accept some input N and generate a pseudo random sequence of length N. Straight forward enough. You can choose to name this whatever you want, or not at all.

3.Be deterministic, the same seed should generate the same sequence each time.

4.I have absolutely no idea how this is tested but the results should be reasonably random. So the results from your generator should be close to uniform distribution within your arbitrary sample space.

5.You can choose any mathematical formula/operation for your pseudo random generation, but it should update its internal state in a deterministic way and use that new internal state for the generation of another number.

You may not outsource your random number generation to any source, be it built in or online. Every variable update/ operation should be explicitly ordered. (things like random.randint() is banned, sorry I only know python)

I can't come up with some inventive way of scoring, so shortest program wins.

user91975

Posted 2020-02-07T09:45:44.017

Reputation: 19

Question was closed 2020-02-07T11:01:25.083

1define "reasonably random". 1234567890123456... looks okay to me. – John Dvorak – 2020-02-07T09:57:56.180

Also, what's the minimum allowable sample size? It's much easier to get uniform distribution across {0,1} than across {0..2^64-1} – John Dvorak – 2020-02-07T10:01:00.080

1That's too trivial. Just take a bad RNG algorithm, like Lehmer generator with small constants, and implement it. The only way it would be interesting, is if you clearly set up a threshold for a qualifying level of randomness or a period. – Andriy Makukha – 2020-02-07T10:01:22.993

1I don't think a uniform distribution is a good criteria for randomness.. I heard that humans are bad at random for this reason, they tend to make distribution uniform and avoid repetitions, where random should do the contrary to a statical extend – Kaddath – 2020-02-07T10:14:45.953

In fact, there's a whole study of getting a good uniform sampling function, and pure random sampling is one of the worst, often getting samples that are clustered close to each other while leaving huge gaps somewhere else - and the latter only gets worse in higher dimensions. – John Dvorak – 2020-02-07T10:17:48.980

2For uniformness I guess f(seed) = seed++ works perfectly if seed is allowed to overflow from 2^64-1 to 0... – my pronoun is monicareinstate – 2020-02-07T10:20:45.233

1

Python 3, 15 bytes lambda x,s=0: 4 it is random, I swear

– RGS – 2020-02-07T10:45:11.797

My arbitrary sample space is the number 0. My code outputs 0 N times. That works, right? – user253751 – 2020-02-07T11:51:47.780

@RGS If you're interested in how a simple RNG works, LCG is about the simplest. Wikipedia has a good description https://en.wikipedia.org/wiki/Linear_congruential_generator

– DeathIncarnate – 2020-02-07T20:51:03.600

No answers