有人问我如何在
1
的范围内添加 random.nextDouble()
的问题。我想出的解决方案是生成 int 并将其应用于小数。
double getRandomDouble(){
final random = Random();
int a = random.nextInt(10001); // generates int from 0 to 10000
String stringValue;
if (a == 10000) {
stringValue = '1.0';
} else {
stringValue = '0.$a';
}
return double.parse(stringValue);
}
有人告诉我解决方案是
random.nextInt(256)/255
或 random.nextInt(intMax)/intMax