# Who's that PRNG?

Given a sequence of 625 32-bit unsigned integers (that is, in the range [0, 2**32)), output which of the following pseudorandom number generators generated the sequence:

Specifically, the C implementations of these three generators used for this challenge are as follows:

#include <stdint.h>

https://en.wikipedia.org/wiki/Linear_congruential_generator
https://en.wikipedia.org/wiki/Xorshift
https://en.wikipedia.org/wiki/Mersenne_Twister
*/

uint32_t lcg_seed;
uint32_t xor_x, xor_y, xor_z, xor_w;

void lcg_srand(uint32_t seed) {
lcg_seed = seed;
}

uint32_t lcg(void) {
lcg_seed = ((uint64_t) lcg_seed * 1103515245 + 12345) & ( (uint64_t) 0xFFFFFFFF  );
return (uint32_t) lcg_seed;
}

void xorshift128_srand(uint32_t x, uint32_t y, uint32_t z, uint32_t w) {
xor_x = x;
xor_y = y;
xor_z = z;
xor_w = w;
}

uint32_t xorshift128(void) {
uint32_t t = xor_x;
t ^= t << 11;
t ^= t >> 8;
xor_x = xor_y; xor_y = xor_z; xor_z = xor_w;
xor_w ^= xor_w >> 19;
xor_w ^= t;
return xor_w;
}

enum {
// Assumes W = 32 (omitting this)
N = 624,
M = 397,
R = 31,
A = 0x9908B0DF,

F = 1812433253,

U = 11,
// Assumes D = 0xFFFFFFFF (omitting this)

S = 7,
B = 0x9D2C5680,

T = 15,
C = 0xEFC60000,

L = 18,

MASK_LOWER = (1ull << R) - 1,
};

static uint32_t  mt[N];
static uint16_t  index;

// Re-init with a given seed
void mt_Initialize(const uint32_t seed) {
uint32_t  i;

mt[0] = seed;

for ( i = 1; i < N; i++ ) {
mt[i] = (F * (mt[i - 1] ^ (mt[i - 1] >> 30)) + i);
}

index = N;
}

static void mt_Twist() {
uint32_t  i, x, xA;

for ( i = 0; i < N; i++ ) {
x = (mt[i] & MASK_UPPER) + (mt[(i + 1) % N] & MASK_LOWER);

xA = x >> 1;

if ( x & 0x1 )
xA ^= A;

mt[i] = mt[(i + M) % N] ^ xA;
}

index = 0;
}

// Obtain a 32-bit random number
uint32_t mt_ExtractU32() {
uint32_t  y;
int       i = index;

if ( index >= N ) {
mt_Twist();
i = index;
}

y = mt[i];
index = i + 1;

y ^= (mt[i] >> U);
y ^= (y << S) & B;
y ^= (y << T) & C;
y ^= (y >> L);

return y;
}


## Rules

• Input and output may be in any unambiguous, consistent format. If you're not sure if a format would be allowed, feel free to ask.
• All inputs are guaranteed to match exactly one PRNG's output (there will never be a case where an input sequence matches none or more than one PRNG's output).

## Test Cases

Due to post length limits, only 3 test cases are included. A larger list of test cases can be found here. Note that the values are provided in hexadecimal.

[CEE63876, DE7E2E77, 54EC3AE4, 92DEBB4D, D0756602, EE9D3B13, 04A42150, 0FE8BF49, 2BD7E04E, BB96756F, B9B2027C, B5750705, 9DD5B35A, E23EF98B, D15ACA68, C1C40E81, 35ABAB26, 2DA1A367, 36462514, 3CA211BD, E79753B2, A49B0F03, B3EA7E80, 64630CB9, BC22F8FE, D535985F, 14B902AC, EE9EBB75, A831A70A, 79C55B7B, F7099D98, 98AC99F1, D9CB29D6, 53C43457, 04C6FB44, 43DFE42D, 05A80D62, D86DBEF3, F9DA87B0, 99839629, 017D9DAE, 0B1B574F, A5586EDC, F2966BE5, B709E6BA, A160196B, D16B9CC8, FF46E161, 3BDFB486, 4CE4E147, BE01BD74, 6A2F329D, 99F29312, C7044AE3, 9A373CE0, 73515B99, 94E2CE5E, FA26B23F, 2883470C, 3ED31855, 7809726A, 64DE335B, 79A3C7F8, 5379E4D1, B9444B36, 92C2AA37, 6A496BA4, 84E6FD0D, FC81E4C2, 470DB2D3, 4F839E10, 45935D09, C40D8B0E, B4F6A92F, FDEC8B3C, 1C8BC0C5, D99B4A1A, E1CEA94B, 17951F28, BAECA441, 3C13EDE6, 0CDC8F27, 4CB105D4, ED1E437D, 1E210272, B8F8F6C3, FB02AB40, 63D09A79, 4178D3BE, B3EA3C1F, 58073B6C, 10B76535, BAEA6DCA, 37807B3B, 11E2A258, 93061FB1, EB299C96, 00719017, 130B8C04, EFAC05ED, 385AEC22, F6F516B3, D4B76470, 915013E9, D45FA86E, C5206B0F, 6C06579C, 4C0D05A5, 9BE1DD7A, 7702A92B, 3DEF5188, E0ED5721, DA205746, 0080AD07, 05EBFE34, 3D27445D, 7D7AA1D2, 44F112A3, 9B64C9A0, 7118C959, 08BD091E, FC7835FF, A1DCDFCC, 1B03A215, 4D2C992A, 9324331B, 0FDE2CB8, C1894A91, B9531DF6, DDC8E5F7, 38A55C64, 59E6FECD, C88B2382, 409BEA93, C48DDAD0, F5F1BAC9, DF4BF5CE, A3909CEF, C43DD3FC, 55D23A85, A035A0DA, 5074190B, CA9233E8, D980FA01, 85DCF0A6, 96C93AE7, B94AA694, 0E02353D, 4D577132, D1649E83, AC759800, D261E839, 7D876E7E, 29C89FDF, 309C342C, D06FCEF5, 1727F48A, 35415AFB, AFAE6718, C53B6571, 3998CF56, 47C0ABD7, C0AEDCC4, D54FE7AD, 486A8AE2, 187A2E73, C61F0130, E8B051A9, DDAA732E, 143F3ECF, 072B005C, CA935F65, 94EE943A, 799AF8EB, 2F95C648, 88DF8CE1, 7B21BA06, 1AAE38C7, E264FEF4, 4F67161D, AF0F7092, 60CB9A63, CB4D1660, BAE3F719, 7EB003DE, EDD379BF, 5ADD388C, 5FB3EBD5, 0D347FEA, F74FF2DB, 196B5178, 00547051, 4DD2B0B6, 3750E1B7, 3CC00D24, AF9EC08D, DF512242, 0F07E253, EC82D790, DAC3D889, 1453208E, 372450AF, 6165DCBC, F7087445, 3464B79A, 19EF48CB, CB1208A8, 4F410FC1, C1C6B366, B327A6A7, A8D30754, 2D0DE6FD, 4FFA9FF2, 919E0643, 010344C0, 59D6F5F9, 370EC93E, 5690C39F, A337ECEC, B387F8B5, 2FAA3B4A, CEC7FABB, 612CEBD8, 510C6B31, A3D8C216, 81718797, EA70ED84, 728B896D, 4096E9A2, 50BD0633, B6D15DF0, 39644F69, 141DFDEE, 8837D28F, 2B86691C, E3E97925, 44F00AFA, 74E908AB, E71EFB08, 08DD82A1, 2DA3DCC6, 632D8487, E02CBFB4, 0EAEA7DD, 6970FF52, 9E53E223, C2B02320, DA72E4D9, 1D7BBE9E, CDF87D7F, B844514C, 72A3F595, 8AE126AA, CD21729B, 870B3638, 119B5611, B5830376, D71A9D77, B3597DE4, E3CE424D, AB93E102, A6119A13, 10229450, 6DC9B649, B9E30B4E, DF71C46F, EA24A57C, 55EE6E05, 98E88E5A, EA00388B, B9D49D68, 0DECE581, 5E913626, 09B7D267, 080A2814, 980158BD, C0CA8EB2, 5D652E03, F16BB180, 63EFC3B9, F4CEE3FE, 1A02A75F, 749A65AC, FFBFE275, 3731420A, 1FD45A7B, 771E3098, 183930F1, C8A974D6, C5442357, 2D11BE44, 051EEB2D, EBA00862, 737D9DF3, 4F8E7AB0, DD2C0D29, 2E7A48AE, 70CA264F, 4DD891DC, CDCF52E5, 0EA641BA, F4ACD86B, 654AEFC8, 32A73861, C066BF86, 61BE9047, 4C034074, A8BDF99D, A75F4E12, 4149E9E3, DA4DEFE0, 19859299, CBE0395E, 5CA7413F, DED22A0C, 7993BF55, 58F28D6A, 1058B25B, 097DDAF8, B71DFBD1, EF241636, B4E61937, 9931AEA4, 1435840D, 58135FC2, B97911D3, 382D1110, E8C35409, E6BBB60E, CC38F82F, 333A2E3C, 884427C5, 9081251A, 2C66E84B, F799F228, C7447B41, 8AFC78E6, 0239BE27, 83B008D4, 5C9C8A7D, FA873D72, 587A15C3, 366EDE40, 1A6C5179, FD87BEBE, 13DE4B1F, 29839E6C, BAD78C35, 207D08CA, 04267A3B, 02423558, BC81B6B1, 06CAE796, EAF87F17, E5514F04, 8ACA0CED, D445E722, 147BF5B3, F9165770, EDC78AE9, A37F536E, DDB63A0F, A2E17A9C, 7E04ECA5, 14D1387A, 44A6682B, 6AD9A488, 97FCAE21, C22A6246, 5E215C07, 32A88134, 0B550B5D, 239A5CD2, 4D6DB1A3, 2AE67CA0, 81DC0059, 309D741E, 199FC4FF, B346C2CC, 5A434915, CA28B42A, 7CB5B21B, 11833FB8, 729C6191, B975E8F6, 887354F7, AB089F64, 1E9485CD, CF8F9E82, BCFE4993, 2D624DD0, 4570B1C9, 719D20CE, ED39EBEF, D16676FC, 63C9A185, 9DEE7BDA, 0CE3580B, A52206E8, ED07D101, 35C87BA6, C46D69E7, 8884A994, 489F7C3D, 17F0AC32, 669CBD83, 48CCCB00, 670C9F39, 57F9597E, A3E3AEDF, 06B3972C, AA8EF5F5, 9E4D8F8A, 177E59FB, D358FA18, 9FA5FC71, 7CFD1A56, 8A4E9AD7, 2FEF9FC4, C14CEEAD, 454885E2, 87780D73, DC28F430, 44F6C8A9, A9ED1E2E, 9EBC0DCF, 1F61235C, AA4A4665, 3A30EF3A, 7095B7EB, 788B1948, 8A9DE3E1, 81AEC506, 6015E7C7, 60DC81F4, E433DD1D, 58E22B92, 867F3963, 8D39C960, DD362E19, B2736EDE, 44A208BF, DA621B8C, BA7292D5, F1439AEA, 8DF871DB, CFDB6478, 85D68751, 93387BB6, C98250B7, 659E5024, A0AB478D, BCC89D42, E4614153, 78824A90, 3D91CF89, F1474B8E, F2349FAF, 19697FBC, 7E3EDB45, 03F0929A, 773587CB, A32CDBA8, B0F6E6C1, 0DB53E66, 3812D5A7, 43480A54, E9CA2DFD, F3C6DAF2, 2B8D2543, 614577C0, F390ACF9, CAE3B43E, E9D2D29F, 10EA4FEC, 54A61FB5, 2362D64A, B59BF9BB, 7B227ED8, E3660231, 0A000D16, FB067697, E9ACB084, 2667906D, 4967E4A2, E031E533, E18650F0, 7C79C669, 3883A8EE, 439BA18F, 78178C1C, C85F6025, 218565FA, 443AC7AB, CF1F4E08, 1C4AD9A1, 0DB3E7C6, 2F5C3387, 635F42B4, A11A6EDD, 81F6BA52, 703E8123, 9A07D620, B5541BD9, 7822299E, DD6E0C7F, B8E4344C, FFE19C95, A10341AA, 7FE0F19B, 35464938, F28C6D11, BB2BCE76, AFD30C77, 05B2C0E4, F839C94D, 8A7E5C02, 2361F913, 624D0750, 4AE6AD49, BC7A364E, 4AE9136F, 2003487C, 2D63D505, C547695A, 171D778B, 927A7068, 04D1BC81, 8182C126, 04EA0167, A0BA2B14, 8DDC9FBD, 28C9C9B2, 0B0B4D03, 7898E480, 29B87AB9, DD06CEFE, C56BB65F, 0CE7C8AC, FEDD0975, E27CDD0A, FA3F597B, 4A5EC398, 6981C7F1, 4C93BFD6, 54E01257, AF488144, F7D9F22D, AB640362, F2697CF3, B1EE6DB0, EE108429, 0602F3AE, 1C14F54F, 21C4B4DC, 0E0439E5, 2D8E9CBA, 4B55976B, 6F5642C8, 1EC38F61, 34F9CA86, 53B43F47, 86F0C374, 6FC8C09D, 99980912, 4E6B88E3, AA10A2E0, 53F5C999, 6869A45E, A3C3D03F, 738D0D0C, 50506655, 6C27A86A, 4E2F315B, F283EDF8, 7A7E12D1, 300FE136, 33258837, 8805F1A4, 43000B0D, 6370DAC2, 2DC070D3, F3828410, A72F4B09, E9F5E10E, 2717472F, B9F3D13C, 86F88EC5, A4B3001A, 585B274B, D3CAC528, 9A585241, BFF103E6, 92B2ED27, 4D9B0BD4, 4296D17D, 11B97872, 28D734C3, 47871140, 33440879, D522A9BE, D66E5A1F, 7F6C016C, AEF3B335, CE5BA3CA, C128793B, 51CDC858, D3B94DB1, A3783296, 6F9B6E17, DD831204, 336413ED, F5FCE222, 51DED4B3, B6214A70, B37B01E9, 892AFE6E, 37E8090F, 51289D9C, 70F8D3A5, 810C937A, D1A6272B, 19EFF788, 23C80521, 86406D46] -> LCG

• A great question ! – user9206 Nov 5 '16 at 10:34

# Jelly, 23 bytes

^Ḋṫ4^^æ«11$æ»31ż&2\ḂS<4  Takes an array of integers; returns [0, 1] for LCG, [1, 0] for Xorshift, and [0, 0] for MT. Try it online! (The permalink might be too long for some browsers.) ### Background The most random thing about the LCG is that it is considered a PRNG. For starters, the lower k bits of the state aren't affected by the higher 32 - k bits, making it an exceptionally poor choice for most applications. In particular, the LSB of an updated state is only affected by the LSB of the previous one, and since 1103515245 and 12345 are both odd, the generated numbers will follow an even-odd-even-odd pattern. The flaws of Xorshift are more subtle, but the generated bits still follow simple linear equations. Given five consecutive generated words wk, wk+1, wk+2, wk+3, wk+4, the (non-linear equation) wk+4 = wk+3 ⊕ wk+3 >> 19 ⊕ wk ⊕ wk ≪ 11 ⊕ (wk ⊕ wk ≪ 11) ≫ 8 holds by definition. If we enumerate the bits of the words (0 being the lowest, 31 being the highest), we can observe that the (linear) equation wk,20 ⊕ wk,31 ⊕wk+3,31 ⊕ wk+4,31 = 0 holds. To verify this, it suffices to observe that words that have been shifted to the right cannot affect wk+4,31, the most significant bit of wk+4. That leaves the Mersenne Twister. While there are better (faster, less biased, or both) PRNGs, identifying data generated by MT19937 is a lot more complicated than it was with LCG and Xorshift. Fortunately, we don't have to. If the data doesn't fit any of the two previous patterns, it was generated by the Mersenne Twister. ### How it works ^Ḋṫ4^^æ«11$æ»31ż&2\ḂS<4  Main link. Argument: A (array of integers)

Ḋ                       Dequeue; drop the first integer.
^                        XOR the k-th element of A with the k-th element of the
previous result, effectively computing the XOR of all
neighboring integers in A.
ṫ4                     Tail; drop the first three elements of the result.
^                    XOR the remaining integers with the corr. integers in A.
æ«11\$              Yield the integers in A, shifted 11 times to the left.
^                   XOR the results to both sides.
æ»31          Shift all results 31 times to the right.
&2\      Yield A, pairwise reduced by bitwise AND.
ż         Zip the results to both sides.
Ḃ     Bit; compute their parities.
S    Sum; add the parities of the results left and right to ż.
<4  Compare the sums with 4.
This is necessary since A and the various modifications
with dropped elements have different lengths, which
introduces a few garbage values.

• ^Ḋṫ4^^ Why do I see too much repetition here? I smell something, but I'm not sure if there is a shorter way than XOR [...] XOR XOR. – Erik the Outgolfer Nov 8 '16 at 17:45
• Out of curiosity, what would the solution look like if you had to identify MT19337 sequences (perhaps if the input wasn't required to be generated from one of the three PRNGs)? – user45941 Nov 20 '16 at 5:20
• @Mego A lot more complicated. I might actually have to implement MT19937. – Dennis Nov 20 '16 at 5:25
• I'd be interested in seeing such a solution, if you're interested in writing it. When I was originally writing the challenge, I considered having the input possibly match none of the three PRNGs (which would require validating against all three, rather than just two). However, I decided not to include that possibility as I worried it would make the challenge too difficult. – user45941 Nov 20 '16 at 5:27