Random float number generation

后端 未结 14 1611
孤城傲影
孤城傲影 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:25

    For C++, it can generate real float numbers within the range specified by dist variable

    #include   //If it doesnt work then use   #include 
    #include 
    
    using namespace std;
    
    typedef std::tr1::ranlux64_base_01 Myeng; 
    typedef std::tr1::normal_distribution Mydist;
    
    int main() { 
           Myeng eng; 
           eng.seed((unsigned int) time(NULL)); //initializing generator to January 1, 1970);
           Mydist dist(1,10); 
    
           dist.reset(); // discard any cached values 
           for (int i = 0; i < 10; i++)
           {
               std::cout << "a random value == " << (int)dist(eng) << std::endl; 
           }
    
           return (0);
    }
    

提交回复
热议问题