How do I generate random numbers in Dart?

前端 未结 14 1443
一个人的身影
一个人的身影 2021-01-31 06:49

How do I generate random numbers using Dart?

14条回答
  •  自闭症患者
    2021-01-31 07:10

    You can achieve it via Random class object random.nextInt(max), which is in dart:math library. The nextInt() method requires a max limit. The random number starts from 0 and the max limit itself is exclusive.

    import 'dart:math';
    Random random = new Random();
    int randomNumber = random.nextInt(100); // from 0 upto 99 included
    

    If you want to add the min limit, add the min limit to the result

    int randomNumber = random.nextInt(90) + 10; // from 10 upto 99 included
    

提交回复
热议问题