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

前端 未结 30 3221
一个人的身影
一个人的身影 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:24

    Simple and efficient:

    int rand7 ( void )
    {
        return 4; // this number has been calculated using
                  // rand5() and is in the range 1..7
    }
    

    (Inspired by What's your favorite "programmer" cartoon?).

提交回复
热议问题