How to generate a random integer number from within a range

后端 未结 11 1228
隐瞒了意图╮
隐瞒了意图╮ 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:53

    unsigned int
    randr(unsigned int min, unsigned int max)
    {
           double scaled = (double)rand()/RAND_MAX;
    
           return (max - min +1)*scaled + min;
    }
    

    See here for other options.

提交回复
热议问题