Generating a random double between a range of values

后端 未结 5 1323
天涯浪人
天涯浪人 2021-01-13 18:33

Im currently having trouble generating random numbers between -32.768 and 32.768. It keeps giving me the same values but with a small change in the decimal field. ex : 27.xx

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-13 19:02

    Also, If you are not using c++ 11 you can use the following function instead:

    double randDouble(double precision, double lowerBound, double upperBound) {
     double random;
     random = static_cast(((rand()%(static_cast(std::pow(10,precision)*(upperBound - lowerBound) + 1))) + lowerBound*std::pow(10,precision)))/std::pow(10,precision);
     return random;
    }
    

提交回复
热议问题