Flutter is an open-source platform that is used to create interactive and efficient mobile applications which are compatible with native operating systems. One of the most common requirements while creating an application's functionalities is to generate a random number. They serve various important purposes for game mechanics, creating unique IDs, or simulating random events.
Random numbers refer to a sequence of unorganized numbers that are generated in such a way that each number in the sequence appears to be completely unrelated to the previous or next number in the sequence.
In Flutter, this random number generation holds great value and importance as its crucial for developing any sense of randomness in the mobile application. There may be times when you want to introduce some uncertainty and unexpectedness within your application. That's where random number generation comes into play.
Flutter uses the Dart programming language as its primary language, and Dart comes along with a built-in dart:math
library that incorporates many mathematical operations, including random number generation.
Here's how you can import it:
import 'dart:math';
The dart:math
library provides the Random
class to generate random numbers within a specified range.
After creating a final object of the Random
class we can use that object to call the nextInt()
nextDouble()
method.
import 'dart:math';int getRandomInteger() {final random = Random();return random.nextInt();}
The function getRandomInteger
will return random integer values within the minimum and maximum 32-bit signed integers, which are:
-2147483648 (minimum 32-bit signed integer)
2147483647 (maximum 32-bit signed integer)
The distribution of the generated integers will be uniform, meaning each possible integer value within the range has an equal probability of being returned.
The nextInt()
method also helps specify the range of random number sequences by using the specified parameter. This confines the random number generation to within a specified range, from the maximum value specified to the minimum value specified.
Here's how you can change the getRandomInteger
function to generate random numbers within a range.
import 'dart:math';int getRandomInt(int min, int max) {final random = Random();return min + random.nextInt(max - min + 1);}
The function mentioned above will return values within the min and max values. This allows the user to reuse this function for various scenarios.
import 'package:flutter/material.dart'; class PageFour extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Page Four'), backgroundColor: Colors.purple,), body: Center( child: Text( 'This is Page Four', style: TextStyle(fontSize: 24), ), ), ); } }
This app gives us an explanation of how random number generation can help us introduce randomness in the designs, layout, and functioning of our app.
In the main.dart
we create a RandonNumberPage
which is a stateful widget. We define all the random functioning inside this class and then use the generated random number to route to specific pages in a random pattern.
Lines 26–32: We define the getRandomInt
function to generate a random number in between the range of one and four. Then we use the setState
function to change the state of our widget and update the randomNum
variable.
Lines 37–55: We define a function the navigateToPage
and use switch cases for various random numbers that could be generated. Then we navigate to the respective page using Navigate.push()
.
Lines 58–61: Then, inside the build
method of our widget, we create an app bar and initialize a column as its child.
Lines 62–86: Inside the column, we use a Text widget to output the random number generated and then create an EleveatedButton
. In the onPressed()
function of this button, we call the getRandomInt()
function and the navigateToPage()
function. This allows us to create a random number and get routed to that respective random page.
Free Resources