How to get a random number from range in dart?

前端 未结 9 1895
情深已故
情深已故 2021-01-03 17:13

How does one get a random number within a range similar to c# Random.Next(int min, int max);

9条回答
  •  猫巷女王i
    2021-01-03 17:58

    This is really late, but this for anyone who still has the question.

    The most easiest way to get a random number between a min and a max is the following :

    import 'dart:math';
    
    int max = 10;
    
    int randomNumber = Random().nextInt(max) + 1;
    

    The math module in dart has a function called nextInt. This will return an integer from 0 (including 0 ) to max - 1 ( exluding max ). I want a number 1 to 10, hence I add 1 to the nextInt result.

提交回复
热议问题