Math in Dart
Math In Dart
It helps you to perform mathematical calculations efficiently. We can use dart math if you want to perform math calculations like generating random number, finding square root, power of a certain number, or round specific numbers etc. First you need to import 'dart:math';
.
How To Generate Random Number In Dart
This example shows how to generate random numbers in a dart from 0 - 9, 1 to 10. After watching this example, you will be able to generate a random number between your choice.
import 'dart:math';
void main()
{
Random random = new Random();
int randomNumber = random.nextInt(10); // from 0 to 9 included
print("Generated Random Number Between 0 to 9: $randomNumber");
int randomNumber2 = random.nextInt(10)+1; // from 1 to 10 included
print("Generated Random Number Between 1 to 10: $randomNumber2");
}
In this program, random.nextInt(10)
function is used to generate random number between
0 to 9 in which the value is store in variable named randomNumber
.
Similiary, random.nextInt(10)+1
function is used to generate random number between
1 t0 9 in which the value is store in variable named randomNumber2
.
Generate Random Number Between Any Number
Use this formula to generate a random number between any numbers in the dart.
Example
min + Random().nextInt(max - min);
}
Example
import 'dart:math';
void main()
{
int min = 10;
int max = 20;
int randomnum = min + Random().nextInt(max - min);
print("Generated Random number between $min and $max is: $randomnum");
}
In this program, minimum value is 10 and maximum value is 20. The random number is generated between 10 to 20 by using above formula.
Random Boolean And Double Value
To generate random boolean value in dart.
Syntax
min + Random().nextBool(); // return true or false
min + Random().nextDouble(); // return 0.0 to 1.0
}
Example
import 'dart:math';
void main()
{
double randomDouble = Random().nextDouble();
bool randomBool = Random().nextBool();
print("Generated Random double value is: $randomDouble");
print("Generated Random bool value is: $randomBool");
}
In this program, the variable named randomDouble
will store 1 value generated from between range of double
i.e 1.7E +/- 308.
Similary, the variable named randomBool
will store value true or false only in the program.
Generate List Of Random Number In Dart
This example will generate a list of 10 random numbers between 1 to 100.
import 'dart:math';
void main()
{
List<int> randomList = List.generate(10, (_) => Random().nextInt(100)+1);
print(randomList);
}
Useful Math Function In Dart
Some useful math function, which you can use to perform your daily task with dart.
Function Name | Output | Description |
---|---|---|
pow(10,2) | 100 | 10 to the power 2 is 10*10 |
max(10,2) | 10 | Maximum number is 10 |
min(10,2) | 2 | Minimum number is 2 |
sqrt(25) | 5 | Square root of 25 is 5 |
Example
import 'dart:math';
void main()
{
int num1 = 10;
int num2 = 2;
num powernum = pow(num1,num2);
num maxnum = max(num1,num2);
num minnum = min(num1,num2);
num squareroot = sqrt(25); // Square root of 25
print("Power is $powernum");
print("Maximun is $maxnum");
print("Minimum is $minnum");
print("Square root is $squareroot");
}
In this program, pow(num1, num2)
is a function where num1 is a digit and num2 is a power.
max(num1,num2)
is a function which give the maximum number between num1 and num2.
min(num1,num2)
is a function which give the mininum number between num1 and num2.
sqrt(25)
is a function which give the square root of 25.