How to generate a random integer number from within a range

后端 未结 11 1243
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 23:57

This is a follow on from a previously posted question:

How to generate a random number in C?

I wish to be able to generate a random number from within a part

11条回答
  •  花落未央
    2020-11-22 00:45

    Here is a formula if you know the max and min values of a range, and you want to generate numbers inclusive in between the range:

    r = (rand() % (max + 1 - min)) + min
    

提交回复
热议问题