From df2f73d802b5b6340c1fc5ec180ae9cb0654322a Mon Sep 17 00:00:00 2001 From: epixian <38962121+epixian@users.noreply.github.com> Date: Fri, 16 Dec 2022 16:45:11 -0500 Subject: [PATCH 1/4] Better random country generation Incorporates the very fast Mulberry32 PRNG algorithm into the "answer" country generation using the UTC date as the seed. --- src/util/answer.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/util/answer.ts b/src/util/answer.ts index 1b5027e..b94c429 100644 --- a/src/util/answer.ts +++ b/src/util/answer.ts @@ -12,10 +12,23 @@ const shuffleAdjust = today < "2022-08-01" ? "5" : "6"; function generateKeyNew(list: any[], day: string) { const [year, month, date] = day.split("-"); const dayCode = Date.UTC(parseInt(year), parseInt(month) - 1, parseInt(date)); - const SHUFFLE_KEY = process.env.REACT_APP_SHUFFLE_KEY || "1"; - const key = - Math.floor(dayCode / parseInt(SHUFFLE_KEY + shuffleAdjust)) % list.length; - return key; + return Math.floor(list.length * mulberry32(dayCode)); +} + +/** + * Mulberry32 PRNG (JS) + * @link https://github.com/bryc/code/blob/master/jshash/PRNGs.md#mulberry32 + * + * Ported from the original C implementation + * @author Tommy Ettinger + * @link https://gist.github.com/tommyettinger/46a874533244883189143505d203312c + * @license Public Domain + */ +function mulberry32(a) { + a |= 0; a = a + 0x6D2B79F5 | 0; + var t = Math.imul(a ^ a >>> 15, 1 | a); + t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; + return ((t ^ t >>> 14) >>> 0) / 4294967296; } const key = generateKeyNew(countryData, today); From 939ce6482b11baec8016390521d82c8227d6c9ca Mon Sep 17 00:00:00 2001 From: epixian <38962121+epixian@users.noreply.github.com> Date: Mon, 13 Mar 2023 17:35:33 -0400 Subject: [PATCH 2/4] Add type --- src/util/answer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/answer.ts b/src/util/answer.ts index b94c429..1725912 100644 --- a/src/util/answer.ts +++ b/src/util/answer.ts @@ -24,7 +24,7 @@ function generateKeyNew(list: any[], day: string) { * @link https://gist.github.com/tommyettinger/46a874533244883189143505d203312c * @license Public Domain */ -function mulberry32(a) { +function mulberry32(a: number) { a |= 0; a = a + 0x6D2B79F5 | 0; var t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; From 204ed6a3784f526238eca9ec01e3cdf569937bd8 Mon Sep 17 00:00:00 2001 From: epixian <38962121+epixian@users.noreply.github.com> Date: Mon, 13 Mar 2023 18:18:39 -0400 Subject: [PATCH 3/4] Add parentheses --- src/util/answer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util/answer.ts b/src/util/answer.ts index 1725912..f4710d9 100644 --- a/src/util/answer.ts +++ b/src/util/answer.ts @@ -26,9 +26,9 @@ function generateKeyNew(list: any[], day: string) { */ function mulberry32(a: number) { a |= 0; a = a + 0x6D2B79F5 | 0; - var t = Math.imul(a ^ a >>> 15, 1 | a); - t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; - return ((t ^ t >>> 14) >>> 0) / 4294967296; + var t = Math.imul(a ^ (a >>> 15), 1 | a); + t = t + Math.imul(t ^ (t >>> 7), 61 | t) ^ t; + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; } const key = generateKeyNew(countryData, today); From 25be1175bbba5d03f4eea5b71942c40de727c524 Mon Sep 17 00:00:00 2001 From: epixian <38962121+epixian@users.noreply.github.com> Date: Mon, 13 Mar 2023 18:22:50 -0400 Subject: [PATCH 4/4] Remove unused --- src/util/answer.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/util/answer.ts b/src/util/answer.ts index f4710d9..9aeef0a 100644 --- a/src/util/answer.ts +++ b/src/util/answer.ts @@ -7,8 +7,6 @@ countryData.sort((a, b) => { return a.properties.FLAG[1].localeCompare(b.properties.FLAG[1]); }); -const shuffleAdjust = today < "2022-08-01" ? "5" : "6"; - function generateKeyNew(list: any[], day: string) { const [year, month, date] = day.split("-"); const dayCode = Date.UTC(parseInt(year), parseInt(month) - 1, parseInt(date));