{

How to Generate Random Number and range of numbers in Dart or Flutter Programming with example


This tutorial shows multiple ways to Generate Random Numbers in the Dart programming language. Generate Random Number between ranges in Dart

A random number is a unique number generated between a minimum and maximum value for multiple requests

The returned number is always between the ranges of values provided.

Dart random Number Generator example

Dart provides dart:math that contains Random class to provide a random generator. It provides the nextInt method takes a maximum value and returns a random number.

int nextInt(int max)

minimum number is taken as zero as a minimum and the maximum number is the given max value.

Here is a simple example to generate Random Generator

import 'dart:math';

main() {
  var random = Random();
  print(random.nextInt(10)); //Random number between 0 to 10
  print(random.nextInt(100)); //Random number between 0 to 100
  print(random.nextInt(200)); //Random number between 0 to 100
  print(random.nextInt(1000)); //Random number between 0 to 1000
}
  • Imported dart:math package into a program using the import keyword
  • Create a Random variable using the var keyword
  • Call nextInt() with seed number.
  • Random Number always generated between 0 to seed number

Output:

4
70
47
294

Dart random Number Generator within a range example

This is a generating random number between min and max value of ranges example.

import 'dart:math';

main() {
  var random = Random();
  int min = 10;
  int max = 20;
  var randomNumber = min + random.nextInt(max - min);
  print("$randomNumber is random number between $min and $max");
  print(randomNumber);
}

Output:

19 is random number between 10 and 20
19

How to generate a random number in dart excluding a particular number?

For example, You want to generate a random number between 0 to 10 and the generated random number must not be 5.

Here is an example program to generate a Random Number exclude given number

import 'dart:math';

main() {
  var random = Random();
  var excludeNumber = 5;
  var randomNumber;
  do {
    randomNumber = random.nextInt(10);
  } while (randomNumber == excludeNumber);
  print(randomNumber);
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example