How do I generate random numbers in Dart?

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

How do I generate random numbers using Dart?

14条回答
  •  忘了有多久
    2021-01-31 07:17

    Not able to comment because I just created this account, but I wanted to make sure to point out that @eggrobot78's solution works, but it is exclusive in dart so it doesn't include the last number. If you change the last line to "r = min + rnd.nextInt(max - min + 1);", then it should include the last number as well.

    Explanation:

    max = 5;
    min = 3;
    Random rnd = new Random();
    r = min + rnd.nextInt(max - min);
    //max - min is 2
    //nextInt is exclusive so nextInt will return 0 through 1
    //3 is added so the line will give a number between 3 and 4
    //if you add the "+ 1" then it will return a number between 3 and 5
    

提交回复
热议问题