randomNextIntNotZero static method

int randomNextIntNotZero(
  1. int max, [
  2. bool condition = true
])

随机一个不为零的整数

参数

  • max 随机数范围,最大值。即1-max中随机
  • condition 其他满足条件

Implementation

static int randomNextIntNotZero(int max, [bool condition = true]) {
  var ran = Random();
  int _remain = ran.nextInt(max);
  bool stop = (_remain == 0);
  while (stop) {
    _remain = ran.nextInt(max);
    stop = !(_remain != 0 && condition);
  }
  return _remain;
}