Skip to content

Instantly share code, notes, and snippets.

@Willshaw Willshaw/random.js
Created Dec 31, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.