c++ 11 输出随机函数,范围内随机数,期望是 mean

int LeruceRand(int min, int mean, int max) {
  std::random_device rd;

  unsigned int random_device_num = rd();
  int r = random_device_num%(max - min + 1);
  if (r < mean) {
    // find number in right half
    return rand()%(max - mean + 1) + mean;
  } else {
    // find number in left half
    return rand()%(mean - min) + min;
  }
}

 

你可能感兴趣的:(c++)