Skip to content

Instantly share code, notes, and snippets.

@beetcb
Last active August 29, 2020 07:02
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 beetcb/bb4086bac08151ba6247b2c48d2a17c7 to your computer and use it in GitHub Desktop.
Save beetcb/bb4086bac08151ba6247b2c48d2a17c7 to your computer and use it in GitHub Desktop.
// Maybe the easiest way => play with dot
// issue!!! => the float after 5 are all token by 5?
/*
function grabRandomDotWay(min, max, toNthDecimals) {
let result = null;
const range = max - min;
result = Math.trunc((range + 1) * Math.random() * (toNthDecimals && (Math.pow(10, toNthDecimals)) || 1));
// eg : result *= 0.01
result = result * Math.pow(0.1, toNthDecimals || 0) + '';
if (toNthDecimals) {
result>range&&(result = `${range}`);
return result.slice(0, result.indexOf('.') - 0 + toNthDecimals + 1) - 0 + min;
} else {
return Math.trunc(result);
}
}
*/
/* here is the improvement version */
function grabRandomDotWay(min, max, toNthDecimals) {
if (toNthDecimals) {
let result = null
const range = max - min
result = Math.trunc(
range *
Math.random() *
((toNthDecimals && Math.pow(10, toNthDecimals)) || 1)
)
// eg : result *= 0.01
result = result * Math.pow(0.1, toNthDecimals || 0) + ''
result > range - Math.pow(0.1, toNthDecimals) && (result = `${range}`)
return (
result.slice(0, result.indexOf('.') - 0 + toNthDecimals + 1) - 0 + min
)
} else {
return Math.trunc((range + 1) * Math.random())
}
}
// Math.trunc
function grabRandomTrunc(min, max, toNthDecimals = 0) {
const range = max - min
let result = (range * Math.random()).toString()
if (toNthDecimals) {
let replaceNum = null
const index = result.indexOf('.') - 0 + toNthDecimals
replaceNum = Math.trunc(('0.' + result.slice(index) - 0) * 11)
if (replaceNum >= 10) {
result = Number(result)
toNthDecimals === 1
? (result = result + 1)
: (result += 0.1 * (toNthDecimals - 1))
result = result.toString().slice(0, index + 1) - 0
} else {
result = Number(
result.replace(result.charAt(index), replaceNum).slice(0, index + 1)
)
}
} else {
result = (max - min + 1) * Math.random()
result = Math.trunc(result)
}
result > range && (result = range)
return result - -min
}
// Evenly dividion
function grabRandomEvenly(min, max, toNthDecimals) {
let result = null
const range = max - min
toNthDecimals || (result = Math.trunc((range + 1) * Math.random()))
if (toNthDecimals !== 0) {
result = (range * Math.random()).toString()
const index = result.indexOf('.') - 0 + toNthDecimals,
replaceNum = Math.trunc(('0.' + result.slice(index) - 0) * 11)
if (replaceNum >= 10) {
result += 0.1 * (toNthDecimals - 1)
result = result.slice(0, index + 1) - 0
} else {
result =
result.replace(result.charAt(index), replaceNum).slice(0, index + 1) - 0
}
}
result > range && (result = range)
return result - -min
}
// Precision test script (10000 * 999 times)
function loopCheck(c, r) {
'use strict'
c++
r += (function check(c, r) {
'use strict'
c++
r += grabRandomDotWay(0, 5, 3)
if (c >= 10000) {
c = 0
return r / 10000
}
return check(c, r)
})(1, 0)
if (c >= 999) {
return r / 999
}
return loopCheck(c, r)
}
console.log(loopCheck(1, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment