Created
April 12, 2019 20:48
-
-
Save madprops/757deb000bdec25776d5036dae58ee6e to your computer and use it in GitHub Desktop.
Random int with min, max, exclude, and seed options
This file contains 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
get_random_int = function(args={}) | |
{ | |
let def_args = | |
{ | |
min: 0, | |
max: 1, | |
exclude: false, | |
seed: Math.random | |
} | |
args = Object.assign(def_args, args) | |
let num = Math.floor(args.seed() * (args.max - args.min + 1) + args.min) | |
if(args.exclude) | |
{ | |
let diff = args.max - args.min | |
let n = num | |
for(let i=0; i<diff*2; i++) | |
{ | |
if(args.exclude.includes(n)) | |
{ | |
if(n + 1 <= args.max) | |
{ | |
n += 1 | |
} | |
else | |
{ | |
n = args.min | |
} | |
} | |
else | |
{ | |
num = n | |
break | |
} | |
} | |
} | |
return num | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment