Skip to content

Instantly share code, notes, and snippets.

@myfreax
Last active April 1, 2017 08:25
Show Gist options
  • Save myfreax/98e8333604418daa04533ebf38be90bf to your computer and use it in GitHub Desktop.
Save myfreax/98e8333604418daa04533ebf38be90bf to your computer and use it in GitHub Desktop.
生成随机范围的数字,字符串等

生成随机范围的数字,字符串等

export  default (quantity,minChar = 5,maxChar = 10,fn) => {
  let randomArray = [];
  while (randomArray.length < quantity){
    let num = Math.ceil(Math.random()*minChar*(maxChar/minChar));
    if (num >= minChar) randomArray.push(fn(num));
  }
  return randomArray;
}

usage

let items = random(30,25,30, num => random(num, 1, 22, index => 'qwertyuiopasdfghjklzxcvbnm'.substr(index, 1))
  .join(''))
  .map(value => content:value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment