在 Flutter (Dart) 中生成 double 包括 1

问题描述 投票:0回答:1

有人问我如何在

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);
  }
flutter algorithm dart random
1个回答
0
投票

有人告诉我解决方案是

random.nextInt(256)/255
random.nextInt(intMax)/intMax

© www.soinside.com 2019 - 2024. All rights reserved.