randomdart

How do I generate random numbers in Dart?


How do I generate random numbers using Dart?


Solution

  • Use Random class from dart:math:

    import 'dart:math';
    
    void main() {
      var rng = Random();
      for (var i = 0; i < 10; i++) {
        print(rng.nextInt(100));
      }
    }
    

    This code was tested with the Dart VM and dart2js, as of the time of this writing.