Git Product home page Git Product logo

dart_rnd's Introduction

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, Hue.green == 120.

rnd.getColor(hue: Hue.red); // red, green, blue, yellow, cyan, magenta

dart_rnd's People

Contributors

gskinner avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.