Rand01可以生成[0,1]的随机数
要是想生成[n,m]的随机数。
用下式即可
n
+
R
a
n
d
01
∗
(
m
−
n
)
n + Rand01 * (m-n)
n+Rand01∗(m−n)
代码如下
#include<iostream>
#include<iomanip>
using namespace std;
const int u = 17;
const int v = 139;
const int base = 256;
double r = 5;
double Rand01(double &r){
double temp1 = u * r + v;
double temp2 = (int) temp1 / base ;
double temp3 = temp1 - temp2 * base;
r = temp3 ;
return r / base ;
}
double Rand(int n ,int m){
return n + Rand01(r) * ( m - n ) ;
}
int main(void){
for(int i = 0 ; i <= 10 ; i++ )
cout << fixed << setprecision(6) << Rand( 10 , 20 ) << endl ;
}