Last active
January 30, 2024 17:49
-
-
Save cdeath/656508495676da17d827a6860e82bb6d to your computer and use it in GitHub Desktop.
euromillions code golf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* euromillions generator code golf. unique numbers. in javascript. because. | |
* change the parameters for games other than euromillions. | |
* paste the code in the browser console and press enter. | |
*/ | |
// non "RFC compliant" version (does not allow collisions between numbers and stars) | |
for(a=[],r=_=>Math.ceil(Math.random()*(i<5?50:12)),i=0;i<7;i++){n=r();while(a.includes(n))n=r();a[i]=n};a; | |
// zero fucks given (globals, no sorting) | |
e=(m,l)=>{for(r=_=>Math.ceil(Math.random()*m),a=[],n=r(),i=0;i<l;i++){while(a.includes(n))n=r();a[i]=n}return a};e(50,5)+'+'+e(12,2); | |
// fuck da police (globals, with sorting) | |
e=(m,l)=>{for(r=_=>Math.ceil(Math.random()*m),a=[],n=r(),i=0;i<l;i++){while(a.includes(n))n=r();a[i]=n}return a.sort((a,b)=>a-b)};e(50,5)+'+'+e(12,2); | |
// pretty with for | |
const r = m => Math.ceil(Math.random() * m); | |
const e = (m, l) => { | |
const a = []; | |
for (let n = r(m), i = 0; i < l; i++) { | |
while (a.includes(n)) n = r(m); | |
a.push(n); | |
} | |
return a.sort((a, b) => a - b); | |
}; | |
e(50, 5) + ' + ' + e(12, 2); | |
// prettier with .reduce() | |
const r = m => Math.ceil(Math.random() * m); | |
const e = (m, l) => [...Array(l)].reduce(a => { | |
let n = r(m); | |
while (a.includes(n)) n = r(m); | |
a.push(n); | |
return a; | |
}, []).sort((a, b) => a - b); | |
e(50, 5) + ' + ' + e(12, 2); | |
// oldschool | |
function r(m) { | |
return Math.ceil(Math.random() * m); | |
} | |
function e(m, l) { | |
for (var a = [], n = r(m), i = 0; i < l; i++) { | |
while (a.indexOf(n) > -1) n = r(m); | |
a.push(n); | |
} | |
return a.sort(function(a, b) { | |
return a - b; | |
}); | |
}; | |
e(50, 5) + ' + ' + e(12, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment