Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Created June 19, 2020 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZiTAL/47b1338c7289f5b4e0d380ef67ecb646 to your computer and use it in GitHub Desktop.
Save ZiTAL/47b1338c7289f5b4e0d380ef67ecb646 to your computer and use it in GitHub Desktop.
js: random near numbers
class RandomNearNumbersClass
{
_random(min, max)
{
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
_getMinMax(value, amount, min, max)
{
let _min = value - amount;
let loop = 0;
while(_min<min && loop<amount)
{
_min++;
loop++;
}
let _max = value+amount;
loop = 0;
while(_max>max && loop<amount)
{
_max--;
loop++;
}
let result = {min: _min, max: _max};
return result;
}
_shuffle(array)
{
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
get()
{
var result = {};
let min = this._params.min;
let max = this._params.max;
let amount = this._params.amount;
amount = (max-min<amount)?max-min:amount;
let r = this._random(min, max);
result.value = r;
let r_min_max = this._getMinMax(r, amount, min, max);
let rmin = r_min_max.min;
let rmax = r_min_max.max;
let numbers = [];
numbers.push(r);
for(var i=0; i<amount; i++)
{
let a;
do
{
a = this._random(rmin, rmax);
}
while(numbers.includes(a));
numbers.push(a);
}
result.numbers = this._shuffle(numbers);
return result;
}
constructor(params)
{
this._params = params;
}
};
function RandomNearNumbers(params)
{
var instance = new RandomNearNumbersClass(params);
return instance.get();
}
module.exports = RandomNearNumbers;
@ZiTAL
Copy link
Author

ZiTAL commented Jun 19, 2020

Erabiltzeko:

window.setInterval(function()
{
	let random_near_numbers = RandomNearNumbers(
    {
        min: 0,
        max: 10,
        amount: 2
    });
    console.log(random_near_numbers);
}, 1 * 1000);

output adibidea:

numbers: Array(3) [ 7, 6, 5 ]
value: 6
​----​----​----​----​----​----​----​----​----​----​----​----
numbers: Array(3) [ 2, 1, 0 ]
value: 0
​----​----​----​----​----​----​----​----​----​----​----​----
numbers: Array(3) [ 9, 5, 7 ]
​value: 7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment