# 如何在 Dart 中生成随机数？

random dart
##### 19个回答
371

` 中的 `
`dart:math`
类：

``````import 'dart:math';

main() {
var rng = Random();
for (var i = 0; i < 10; i++) {
print(rng.nextInt(100));
}
}
``````

147

``Random``

``random.nextInt(max)``

``dart:math``

``nextInt()``

``0``

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

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

36

``````import 'dart:math';

void main() {

int random(int min, int max) {
return min + Random().nextInt(max - min);
}

print(random(5, 20)); // Output : 19, 5, 15.. (5 -> 19, 20 is not included)
}
``````

30

``````import 'dart:math';

main() {
var rng = new Random();
var l = new List.generate(12, (_) => rng.nextInt(100));
}
``````

27

``dart:math``

``````new Random.secure()
``````

``dart:math``
``Random``

``secure``

``window.crypto.getRandomValues()``

``urandom``

19

``````int random() {
final ary = new Int32Array(1);
window.crypto.getRandomValues(ary);
return ary[0];
}
``````

9

``````static var _random = new Random();
static var _diceface = _random.nextInt(6) +1 ;
``````

``````GestureDetector(
onTap: () {
setState(() {
_diceface = _random.nextInt(6) +1 ;
});
},
child: ClipRRect(
clipBehavior: Clip.hardEdge,
child: Image(
image: AssetImage('images/diceface\$_diceface.png'),
fit: BoxFit.cover,
),
)
),
``````

8

``new Random().nextInt(100); // MAX = number``

``````import 'dart:math';
int MAX = 100;
print(new Random().nextInt(MAX));`
``````

7

``````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
``````

6

``````Random objectname = Random();
int number = objectname.nextInt(100);
// it will generate random number within 100.
``````

3

3

``````class RandomNormal {
num     _min, _max,  _sum;
int     _nEle, _seed, _hLim;
Random  _random;
List    _rNAr;

//getter
List get randomNumberAr => _rNAr;

num _randomN() {
int r0 = _random.nextInt(_hLim);
int r1 = _random.nextInt(_hLim);
int r2 = _random.nextInt(_hLim);
int r3 = _random.nextInt(_hLim);

num rslt = _min + (r0 + r1 + r2 + r3) / 4000.0;  //Add the OS back in...
_sum += rslt; //#DEBUG ONLY
return( rslt );
}

RandomNormal(this._nEle, this._min, this._max, [this._seed = null]) {
if (_seed == null ) {
Random r = new Random();
_seed    = r.nextInt(1000);
}
_hLim   = (_max - _min).ceil() * 1000;
_random = new Random(_seed);
_rNAr   = [];
_sum    = 0;//#DEBUG ONLY

h2("RandomNormal with k: \${_nEle}, Seed: \${_seed}, Min: \${_min}, Max: \${_max}");//#DEBUG ONLY
for(int n = 0; n < _nEle; n++ ){
num randomN = _randomN();
//p("randomN  = \${randomN}");
LIST_add( _rNAr, randomN );
}

h3("Mean = \${_sum/_nEle}");//#DEBUG ONLY
}
}

new RandomNormal(1000, 80, 120);
new RandomNormal(1000, 80, 120);
new RandomNormal(1000, 80, 120);
new RandomNormal(1000, 80, 120);
new RandomNormal(1000, 80, 120);
new RandomNormal(1000, 80, 120);
new RandomNormal(1000, 80, 120);
``````

_swarmii

3

``````import 'dart:math';
Random rnd = new Random();
r = min + rnd.nextInt(max - min);
//where min and max should be specified.
``````

3

``import 'dart:math';``

``````int randomInt(int min, int max) {
return Random().nextInt(max - min + 1) + min;
}
``````

``randomInt(-10,10)``

2

`````` main(){
print("Sequence Number");
oddnum(10).forEach(print);
}
Iterable<int> oddnum(int num) sync*{
int k=num;
while(k>=0){
if(k%2==1){
yield k;
}
k--;
}
}
``````

2

``````import 'dart:math';

void main() {
int max = 10;
int RandomNumber = Random().nextInt(max);
print(RandomNumber);
}
``````

2

``````import 'dart:math';

print(Random().nextInt(100));
``````

2

``````import 'dart:math';

void main() {

Random random = Random();
int randomNumber = random.nextInt(5) + 1;
print(randomNumber);

}
``````

0

``````import 'dart:math';

print(Random().nextDouble());
``````