Last active
October 18, 2024 14:31
-
-
Save annibal/423daa500b98083a3cd81a88abfdb32f to your computer and use it in GitHub Desktop.
JS Helpers for Random
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
/** | |
* clampAndSeparateStringInBlocks | |
* Separates a string in blocks by appending an separator character, and clamps the string to a desired length. | |
* | |
* @param {string} str input text | |
* @param {string} cfg.sep Separator, default "-" | |
* @param {number} cfg.len Desired length. Will slice if smaller than `str`, or prepend with `pad` if larger than `str` length. | |
* @param {number} cfg.siz Size (in characters) of each block. default 4 | |
* @param {string} cfg.pad Padding character, to prepend when `str.length < len`. Default "0". | |
* @returns string. | |
* | |
* @example | |
* clampSeparate("000653K4YNIPHS00") | |
* == "0006-53K4-YNIP-HS00" | |
* | |
* clampSeparate("653K4YNIPHS00") | |
* == "0006-53K4-YNIP-HS00" | |
* | |
* clampSeparate("ABCDEFGHI", { len: 10, siz: 3, sep: ",", pad: "_" }) | |
* == '_AB,CDE,FGH,I' | |
* | |
* ((fourLetterListWithBob) => { | |
* const arr = fourLetterListWithBob.filter(Boolean).sort( (a,b) => a.length - b.length); | |
* const cfg = { sep: " | ", len: arr.length * 4, siz: 4, pad: " " } | |
* return clampSeparate(arr.join(""), cfg); | |
* })( ["jack","jeff","bob","jhon","alyx","zero",""] ) | |
* ' bob | jack | jeff | jhon | alyx | zero' | |
* | |
*/ | |
function clampSeparate(_str, cfg = {}) { | |
const { sep = "-", len = 16, siz = 4, pad = "0" } = cfg; | |
const str = _str.toString().split("").reverse(); | |
return Array(len) | |
.fill(null) | |
.map((_, idx) => { | |
const n = idx + 1; | |
const strToAppend = n % siz === 0 && idx < str.length - 1 ? sep : ""; | |
const char = str.slice(len - n)[0] || pad; | |
return char + strToAppend; | |
}) | |
.join(""); | |
} | |
clampSeparate("ABCDEFGHI", { len: 10, siz: 3, sep: ",", pad: "_" }); | |
/** | |
* | |
* @param {number} max Maximum random number. default 999.999. | |
* @param {number} min Minimum random number. default 100.000. | |
* @param {string} len Maximum output string length, not counting the separators. | |
* @param {number} b_s Block_Size: The separator will be placed amongst blocks of {b_s} characters. default 4. | |
* @param {string} s_c Separator_Character: what to separate with. defatul "-". | |
* @returns string - A random hash. | |
*/ | |
const getKeyTosco = (max = 10 ** 7 - 1, min = 10 ** 6, len = 16, b_s = 4, s_c = "-") => | |
// const getKeyTosco = (max: number = 10 ** 7 - 1, min: number = 10 ** 6, len: number = 16, b_s: number = 4, s_c: string = "-"): string => | |
Math.floor(1 * (Math.random() * max - min + min) * (Date.now() / 1000) * (performance.now() / 1000)) | |
.toString(36) | |
.toUpperCase() | |
.slice(0, len) | |
.padStart(len, "0") | |
.split("") | |
.map((char, i) => ((i + 1) % b_s === 0 ? char + s_c : char)) | |
.join("") | |
.replace(new RegExp(`${s_c}$`, "g"), "") | |
.replace(new RegExp(`^${s_c}`, "g"), ""); | |
// Optional, format the result like "XXX-XXXX" | |
// .slice(6, -5); |
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
function testRandom(fn) { | |
let spread = {}, min = Infinity, max = -Infinity, average = 0; | |
for (let i=0; i<100000;i++) { | |
var ind = fn(); | |
min = Math.min(min, ind) | |
max = Math.max(max, ind) | |
average += ind | |
spread['_'+ind] = (spread['_'+ind] == null ? 1 : spread['_'+ind]+1) | |
} | |
return {min:min, max:max, average:average/100000, spread:spread} | |
} | |
function randomRange(min, max, decimal) { | |
decimal = (decimal == null || decimal < 0) ? Math.pow(10,0) : Math.pow(10,decimal); | |
min = (min == null ? 0 : min) * decimal; | |
max = (max == null ? 100 : max) * decimal; | |
return Math.floor((Math.random() * (max - min)) + min) / decimal | |
} | |
function randomArray(_arr) { | |
var arr = _arr.filter(x => x != null) | |
var l = arr.length; | |
var r = null; | |
var safe = 1000; | |
while (r == null && safe-- > 0) { | |
r = arr[randomRange(0, l+1)] | |
} | |
if (safe < 1) { | |
throw new Error("Maximum call stack exceeded, no not-null value found") | |
} | |
return r; | |
} | |
function shuffleArray(arr) { | |
var r = new Array(arr.length).fill('') | |
var l = arr.length; | |
while(l > 0) { | |
r.push(arr.splice(randomRange(0,l),1)[0] ); | |
l = arr.length | |
} | |
return r | |
} | |
function acronym(pArr) { | |
pArr = pArr.map(words => words.map(word => word.charAt(0) )) | |
var results = []; | |
for (let i=0; i<1000; i++) { | |
var result = ''; | |
var brokeSafe = false; | |
pArr.forEach(letters => { | |
var chosenLetter; | |
var hasLetter = true; | |
var safe = 1000; | |
while (hasLetter && safe-- > 0) { | |
chosenLetter = randomArray(letters); | |
hasLetter = result.indexOf(chosenLetter) != -1; | |
} | |
if (safe < 1) { | |
brokeSafe = true; | |
} | |
result = result + chosenLetter; | |
}) | |
result = shuffleArray(result.split('')).join('') | |
if (!brokeSafe) { | |
if (!results.some(_result => _result == result)) { | |
results.push(result) | |
} | |
} | |
} | |
return results | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment