Skip to content

Instantly share code, notes, and snippets.

@Willshaw
Created December 31, 2019 16:06
Show Gist options
  • Save Willshaw/197ede0c49e07b6e2a3e80025df66230 to your computer and use it in GitHub Desktop.
Save Willshaw/197ede0c49e07b6e2a3e80025df66230 to your computer and use it in GitHub Desktop.
Create a string of random numbers seperated by your mask
// simply returns a single random number
const singleRandom = function singleRandom() {
return (Math.random() * 10).toFixed(0);
};
/*
accept a mask of seperators, e.g. / or //::
and return the mask strings surrounded by numbers.
- if the seperator changes, add a space
- / becomes X/Y
- //:: becomes X/Y/Z A:B:C
*/
const makeRandom = function makeRandom(mask) {
/*
mask will be some characters, e.g. / or //::
split this, and insert a random character between each one.
if the mask chracter changes, add a space
e.g. 1/2/3 4:5:6
*/
const arr_mask = mask.split('');
const cnt_mask = arr_mask.length;
if (cnt_mask) {
const new_string = [];
let current_mask = '';
for (let i = 0; i < cnt_mask; i += 1) {
const next_mask = arr_mask[i];
if (next_mask !== current_mask) {
new_string.push(` ${singleRandom()}`);
current_mask = next_mask;
}
new_string.push(current_mask + singleRandom());
}
// return singleRandom() + arr_mask.map(item => item + singleRandom()).join('');
return new_string.join('');
}
return singleRandom();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment