mercredi 15 novembre 2023

Generate double including 1 in Flutter (Dart)

I've been asked a question how to add 1 in range for random.nextDouble(). The solution I came up with was generating int and applying it to decimals.

  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);
  }



Aucun commentaire:

Enregistrer un commentaire