搜索插件、代码仓与 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

所属分类

源码仓库

使用此插件的项目