How to generate random numbers in Flutter using Dart

Flutter Jun 30, 2023

Random number generation is a fundamental aspect of programming. With Dart's dart:math library, generating random numbers becomes simple. In this post, we will explore how to generate random numbers both integers and doubles in Flutter.

1. Generating random integers

To generate random integers in Flutter, we can use the nextInt function from the Random class. First, import the dart:math package, which is already included in the Dart SDK. Here's an example code snippet:

import 'dart:math';

void main() {
  Random random = Random();

  int randomNumber = random.nextInt(100);

  print(randomNumber);
}

In this code, we create an instance of the Random class and use the nextInt function to generate a random integer between 0 and 99. The nextInt() method in Dart's Random class generates a random integer up to, but not including, the specified maximum value.

Running the code will produce a random integer value, such as:

86

Adding minimum value for integers

You can also add a minimum value to generate a random integer within a specific range. For example:

import 'dart:math';

void main() {
  Random random = Random();

  int randomNumber = random.nextInt(100) + 50;

  print(randomNumber);
}

In this code, we add 50 to the generated random integer, resulting in a range between 50 and 149. Adding the minimum value increases the maximum value accordingly.

Running the code will produce a random integer value, such as:

127

If you want a integer between 50 and 100, adjust the code as follows:

import 'dart:math';

void main() {
  Random random = Random();

  int randomNumber = random.nextInt(51) + 50;

  print(randomNumber);
}

Here, the nextInt function generates a random integer between 0 and 50, and we add 50 to the result. This ensures a random integer between 50 and 100. Note that the maximum value of 101 is excluded.

2. Generating random doubles

In addition to integers, we can generate random doubles using the same library. Change the nextInt function to nextDouble and multiply the result by the desired maximum value. Here is an example:

import 'dart:math';

void main() {
  Random random = Random();

  double randomNumber = random.nextDouble() * 101;

  print(randomNumber);
}

This code will generate a random double between 0 and 100, such as:

66.91117902537108

Adding a minimum value for doubles

To generate a random double within a specific range, you can also add a minimum value. Consider the following code:

import 'dart:math';

void main() {
  Random random = Random();

  int min = 50;
  int max = 100;

  double randomNumber = random.nextDouble() * (max - min + 1) + min;

  print(randomNumber);
}

In this code, we multiply the generated random double by the difference between the maximum and minimum values (plus 1), which creates a range from 0 to 51. Then, we add the minimum value of 50 to shift the range to start from 50 instead of 0.

The resulting generated double will be a double between 50 and 100, such as:

100.8949738371277

Conclusion

Generating random numbers in Flutter using Dart's dart:math library is easy and flexible. By using the Random class and its functions, we can generate random integers and doubles without any problems. By adding minimum values, you can customize the range of generated numbers to suit your needs.

Tags