Generate random float between two floats

前端 未结 4 1069
陌清茗
陌清茗 2021-02-03 22:03

I know this is a rather simple question, but I\'m just not too good at maths.

I know how to generate a random float between 0 and 1:

float random = ((flo         


        
4条回答
  •  抹茶落季
    2021-02-03 22:58

    Random between 2 float :

    float    random_between_two_int(float min, float max)    
    {    
        return (min + 1) + (((float) rand()) / (float) RAND_MAX) * (max - (min + 1));    
    }
    

    Random between 2 int :

    int    random_between_two_int(float min, float max)    
    {    
        return rand() % (max - min) + min + 1;     
    }
    

提交回复
热议问题