Expand a random range from 1–5 to 1–7

前端 未结 30 2842
一个人的身影
一个人的身影 2020-11-22 07:29

Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.

  1. What is a simple so
30条回答
  •  悲&欢浪女
    2020-11-22 08:13

    just scale your output from your first function

    0) you have a number in range 1-5
    1) subtract 1 to make it in range 0-4
    2) multiply by (7-1)/(5-1) to make it in range 0-6
    3) add 1 to increment the range: Now your result is in between 1-7
    

提交回复
热议问题