Question

How do I generate random numbers using Dart?

Was it helpful?

Solution

Use Random class from dart:math:

import 'dart:math';

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

This code was tested with the Dart VM and dart2js, as of the time of this writing.

OTHER TIPS

If you need cryptographically-secure random numbers (e.g. for encryption), and you're in a browser, you can use the DOM cryptography API:

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

This works in Dartium, Chrome, and Firefox, but likely not in other browsers as this is an experimental API.

Here's a snippet for generating a list of random numbers

import 'dart:math';

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

This will generate a list of 12 integers from 0 to 99 (inclusive).

A secure random API was just added to dart:math

new Random.secure()

dart:math Random added a secure constructor returning a cryptographically secure random generator which reads from the entropy source provided by the embedder for every generated random value.

which delegates to window.crypto.getRandomValues() in the browser and to the OS (like urandom on the server)

An alternative solution could be using the following code DRandom. This class should be used with a seed. It provides a familiar interface to what you would expect in .NET, it was ported from mono's Random.cs. This code may not be cryptography safe and has not been statistically tested.

Just wrote this little class for generating Normal Random numbers... it was a decent starting point for the checking I need to do. (These sets will distribute on a "bell" shaped curve.) The seed will be set randomly, but if you want to be able to re-generate a set you can just pass some specific seed and the same set will generate.

Have fun...

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);

Then you can just use it like this to check the mean of sets of 1000 nums generated between a low and high limit. The values are stored in the class so they can be accessed after instantiation.

_swarmii

use this library http://dart.googlecode.com/svn/branches/bleeding_edge/dart/lib/math/random.dart provided a good random generator which i think will be included in the sdk soon hope it helps

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(100) + 10; // from 10 upto 99 included
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top