JS Generates a Simple Instance of a Non repeating Random Array

  • 2021-07-02 23:09:11
  • OfStack

JS Generates a Simple Example of Non-repeating Random Arrays


// Gets the random number in the array 
//HF.Math.RandomNumbers Is a prefix, you can define their own, mainly look at the logic code 
HF.Math.RandomNumbers = function (startNum, endNum, count, repeat) {
  var ret = [];
  if (repeat) {
    for (var i = 0; i < count; i++) {
      ret[i] = HF.Math.Random(startNum, endNum);
    }
    return ret;
  }
  var tmp = [];
  var i = 0;
  for (var s = startNum; s <= endNum; s++) {
    tmp[i++] = s;
  }
  var l = tmp.length;
  for (i = 0; i < count; i++) {
    ret[i] = HF.Array.Remove(tmp, HF.Math.Random(0, --l));
  }
  return ret;
}

Related articles: