How to generate random number in dart program and also Generating a random number within a range in dart code
In this dart tutorial (article), we will learn how to generate random number in dart.
In Dart programming, A Random number are often used in flutter application where a user can generate a unique key number.
What is Random Number in dart program?
In dart, A Random Number is a randomly generated from a large set of numbers & select a number using some mathematical algorithm.
Generating random number are commonly implemented in many flutter application having a feature like OTP verification for user SignIn.
Dart Random class – In math package
In dart, we have a class named “Random” which is used to generate random integer number, bool , random decimal number.
Note: To use Random class in dart, we must import ‘dart:math’ package because Random Class is defined in dart:math package.
It can generate random booleon, integer and double value by using its method provided.
Method on Math.random class
Method | Description |
nextBool() => | Generate either true or false randomly. |
nextDouble() => | Generate an positive floating point number ranging from 0.0 to 1.0. |
nextInt(int max) => | Generate a positive number range from 0 to max (as defined) Eg: random.nextInt(6); // Here 6 is a range, number will generate from 0 – 6. |
Dart Porgram to generate random number bool, int, double
1. Dart Program to generate random number
import 'dart:math';
import 'package:flutter/material.dart';
void main(){
//instantinate random class from dart:math packages
Random randomclass= Random();
//below code Random number betwen 0-50
print(randomclass.nextInt(50));
//below code Randomly generate true or false
print(randomclass.nextBool());
//below code random double betwen 0.0-1.0
print(randomclass.nextDouble());
}
In this example, we are going to show you the simple way to generate random intiger numbers , double values , within the minimum and maximum range. See the example below for more details.
import 'dart:math';
int random = Random().nextInt(1000); //1000 is MAX value
//generate random number below 1000
int random = Random().nextInt(1000); //1000 is MAX value
//generate random number below 1000
Here is the full code Snipet to generate a intiger number form 0-100
import 'dart:math';
import 'package:flutter/material.dart';
void main(){
//generating Random Intiger
Random randomclass=Random();
int generatedNumber= randomclass.nextInt(100); //generate betwen 0-100
}
double randomdouble = Random().nextDouble();
//generate random double within 0.00 - 1.00;
randomdouble = double.parse(randomdouble.toStringAsFixed(4));
//toStringAsFixed will fix decimal length to 4, 0.3454534 = 0.3454
here is the full code snipet to generate Random double value from 0.00 to 1.00
import 'dart:math';
import 'package:flutter/material.dart';
void main(){
//generating Random Intiger
Random randomclass=Random();
//generate double number from 0.00 to 1.00
double randomdouble= randomclass.nextDouble();
//to display only specific digit after a decimal point use the
//following code
double only4digit= double.parse(randomdouble.toStringAsFixed(4));
print(only4digit);
//only 4 digit is dispalyed after decimal point
}
int randomminmax = min + Random().nextInt(max - min);
//generate random number within minimum and maximum value
Here is the full code Snipet to generate Number betwen min - max value
import 'dart:math';
import 'package:flutter/material.dart';
void main(){
Random randomclass= Random();
//generates number betwen 50-100
int BetwenMinAndMax= 50+ randomclass.nextInt(100-50);
print(BetwenMinAndMax);
}
Leave a Comment