How do I generate random numbers in Dart?

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

How do I generate random numbers using Dart?

14条回答
  •  一整个雨季
    2021-01-31 07:18

    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.

提交回复
热议问题