by @kodeazy
flutter How to get negitive random number between specific range using dart file?
- In this blog we will discuss on how to get
negitive
onlyrandom
number between specific range in flutter. -
Below is the syntax for getting
random
number in dart file.Random().nextInt(negitiveNumber)-negitiveNumber
- In the above syntax in place of
negitiveNumber
we add a number greater than0
. - If suppose if we want to print negitive number upto
-50
inRandom().nextInt(50)
we add50
as parameter so that random number is generated uptill50
now we subtract the random number by50
to get the result. -
Below is the example statement.
Random().nextInt(50) - 50
- In the above statement as we subtract
50
from the totalsum
random number is printed between the range of0
to-50
. -
Blow is an example program to print
Random
number between0
to-50
.import 'dart:math'; void main() { print(Random().nextInt(0 + 50) - 50); }
Output:
-10