Skip to content

Instantly share code, notes, and snippets.

@madprops
Created April 12, 2019 20:48
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 madprops/757deb000bdec25776d5036dae58ee6e to your computer and use it in GitHub Desktop.
Save madprops/757deb000bdec25776d5036dae58ee6e to your computer and use it in GitHub Desktop.
Random int with min, max, exclude, and seed options
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