Dart – How to generate random number

In this post, I will share you how to create random numbers (integer and double) using dart:math’ library.

import 'dart:math';

void main() {
  createRandomIntegers();
  print('-----------');
  createRandomDoubles();
}

/// Create and print random integers
void createRandomIntegers() {
  final random = Random();
  for (var i = 0; i < 5; i++) {
    print(random.nextInt(1000));
  }
}

/// Create and print random doubles
void createRandomDoubles() {
  final random = Random();
  for (var i = 0; i < 5; i++) {
    print(random.nextDouble());
  }
}


Output

670
888
963
542
742
-----------
0.8250093360123936
0.04376108344914753
0.5028197730942902
0.18371675284290567
0.8796298313678985

Tagged : / / /
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x