• 开箱即用版
function sectionRandom (start, end) {
  let nums = end - start + 1
  return Math.floor(Math.random() * nums + start)
}

sectionRandom(1, 5)
sectionRandom(2, 8)
  • 工厂函数版
function createSectionRandomFactor (start, end) {
  return () => {
    let nums = end - start + 1
    return Math.floor(Math.random() * nums + start)
  }
}

const one2five = createSectionRandomFactor(1, 5)
one2five()