rnd
Random 的擴展方法與輔助工具,以及一個全域可存取的實例。例如 rnd(10),rnd.getItem(list),rnd.getBit(0.8)。
Dart 中 Random 的擴展方法與輔助工具,以及一個全域可存取的實例。例如:rnd(10),rnd.getItem(list),rnd.getBit(0.8)。
{"sdk":"flutter"}{"sdk":"flutter"}以下為英文專案原文快照,最新內容請造訪 GitHub。
Makes working with random values in Dart / Flutter easier:
To make it easy to set up and propagate a Random instance throughout your app, the rnd package exposes a
global instance via the rnd property. This makes generating a random value as simple as:
import 'package:rnd/rnd.dart';
rnd(10); // random double between 0-10
rnd.getBool(0.8); // 80% chance to get true
You can also get and set the seed for the global instance via rndSeed.
Adds the following methods to all instances of Random. Read the docs for more info.
getInt(min, max, {curve}) // see "Curves" belowgetDouble(min, max, {curve})getBool(chance)getBit(chance) // 0 or 1getSign(chance) // -1 or 1getDeg() // 0-360getRad() // 0-2pigetColor({...}) // see docs for paramsgetItem(list, {remove, curve})shuffle(list, {copy}) // randomize listIt also defines a call method, which lets you get a random double value by calling a Random instance directly:
Random myRandom = new Random();
print(myRandom()); // double between 0-1
print(myRandom(10)); // 0-10
print(myRandom(5,10)); // 5-10
This pairs well with the global instance for quickly getting random values:
new Point(rnd(maxX), rnd(maxY))
The getDouble, getInt, and getItem methods support a curve param which transforms the value distribution. For example:
rnd.getInt(0, 100, curve: Curves.easeIn)
This would favor values nearer to 0, whereas easeOut would favor values nearer to 100. The included example app visualizes the effect of different curves.
The included Hue class provides named hue values for use with getColor. For example:
rnd.getColor(hue: Hue.red); // red, green, blue, yellow, cyan, magenta