搜尋套件、儲存庫與 APP 分類

套件詳情v0.2.0

rnd

Random 的擴展方法與輔助工具,以及一個全域可存取的實例。例如 rnd(10),rnd.getItem(list),rnd.getBit(0.8)。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1410
喜歡數
33
Pub 分數
160

安裝套件

flutter pub add rnd

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

rnd 套件透過擴展方法與實用函數增強了 Dart 的 Random 類別,提供一個全域可存取的隨機實例,讓程式碼更簡潔易讀。它支援簡單的操作,例如產生隨機數(rnd(10))、從清單中選擇隨機項目(rnd.getItem(list)),以及產生有偏移的位元(rnd.getBit(0.8))。專為遊戲、模擬與資料洗牌設計,此套件能無需重複程式碼地簡化常見的隨機化任務。該套件輕量且相容於所有主要平台。

適用情境

  • 遊戲開發
  • 隨機項目選擇
  • Simulations
  • 資料打亂
  • 程序化內容產生

主要特色

  • 全域隨機實例
  • Random 上的擴展方法
  • 簡單的語法,例如 rnd(10)
  • 對加權選擇的支援
  • 跨平台相容性

適合對象

  • 尋求快速隨機實用工具的開發人員
  • 遊戲和應用創作者
  • 原型設計與測試

使用建議

  • 不適合用於加密隨機性
  • 僅限非安全關鍵的使用案例
  • 需要了解基本的隨機化概念

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

rnd package

Makes working with random values in Dart / Flutter easier:

  1. It provides an easy to access, global instance of Random.
  2. It adds helpful extension methods to Random.

Global instance

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.

Extension methods on Random

Adds the following methods to all instances of Random. Read the docs for more info.

  • getInt(min, max, {curve}) // see "Curves" below
  • getDouble(min, max, {curve})
  • getBool(chance)
  • getBit(chance) // 0 or 1
  • getSign(chance) // -1 or 1
  • getDeg() // 0-360
  • getRad() // 0-2pi
  • getColor({...}) // see docs for params
  • getItem(list, {remove, curve})
  • shuffle(list, {copy}) // randomize list

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

Curves

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.

Hue

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

所屬分類

原始碼儲存庫

使用此套件的專案