Generate random number in a range [0, n) in C?

后端 未结 4 977
小蘑菇
小蘑菇 2021-01-26 01:13

Looking to make a really simple random number generator method in C. The numbers should be between 0 and 24 and can be for example 14.5f.

Any help would be great, thanks

4条回答
  •  春和景丽
    2021-01-26 01:38

    float getRand() {
      float rnd = rand();
      rnd /= RAND_MAX;
      return rnd * 24.0f;
    }
    

    Make sure you seed the random number generator with srand before use.

提交回复
热议问题