Random float number generation

后端 未结 14 1337
孤城傲影
孤城傲影 2020-11-22 05:05

How do I generate random floats in C++?

I thought I could take the integer rand and divide it by something, would that be adequate enough?

14条回答
  •  长发绾君心
    2020-11-22 05:39

    Call the code with two float values, the code works in any range.

    float rand_FloatRange(float a, float b)
    {
        return ((b - a) * ((float)rand() / RAND_MAX)) + a;
    }
    

提交回复
热议问题