Dart package for random strings and numbers, with weights

Related tags

Utilities randomness
Overview

randomness

Dart package for random strings and numbers, with weights

Random strings, numbers. RNG with weights. Cryptographically secure options.

Getting started

Add randomness: ^0.1.0 to pubspec.yaml and paste import 'package:randomness/randomness.dart';

Usage

Random Numbers

The following are equivalent to Random().nextInt(5). The Sets include and exclude can have points, or intervals with square brackets.

Randomness.randomInt(include: {0,1,2,3,4})
Randomness.randomInt(include: {[0,4]})
Randomness.randomInt(include: {0,1,[2,4]})
Randomness.randomInt(include: {[0,100]}, exclude: {5,[6,100]})

default is 1 to 1000 inclusive:

Randomness.randomInt()
//681

Cryptographically secure

1 through 100 inclusive, cryptographically secure:

Randomness.randomInt(include: {[1,100]}, cryptographicallySecure: true)
//30

random double, cryptographically secure:

Randomness.randomDouble(min: -3, max: -1, cryptographicallySecure: true)
//-1.7392379311552753

Weighted probability

Give weights. 2/3 chance of 0, 1/3 chance of 1, 2, 3, or 4:

Randomness.randomInt(include: {0,1,2,3,4}, weights: {0: 2, everythingElse: 1})

Ignores included elements not in weights. This will always give 0:

Randomness.randomInt(include: {0,1,2,3,4}, weights: {0:1})

Random Strings

Default length is 10. Default doesn't generate spaces:

Randomness.randomString()
/*
-((f)4$=r4
*/

Strings include numbers, uppercase, lowercase, symbols, and spaces. This will generate numbers and spaces.

Randomness.randomString(length: 100, includeSpaces: true, excludeSymbols: true, excludeUppercase: true, excludeLowercase: true)
//3828 5491 304 0065273406 9 54745452 657207928  4469 078258338317555697369931555479 28159170 32561895

cryptographically secure String with half A's:

Randomness.randomString(length: 50, cryptographicallySecure: true, weights: {'A': 1, everythingElse: 1})
//ACAAAAAABAAAp3litAAAAAAwAAAAAAl$AEe4IAAtAA*A#)AA6[

random element from List, weights may not work with certain data types.

Randomness.randomFromList([{1,2}, 'a', 3], weights: {'a': 1, everythingElse: 1}, cryptographicallySecure: true)
//a

Random n-digit number. Returns a String since parsing would ignore 0's at the beginning, and cannot parse very large integers. The following gives average of half 4's, half 5, 6, 7, 8, or 9:

Randomness.randomNDigits(numberOfDigits: 100, excludeDigits: {0,1,2,3}, weights: {4:1, everythingElse:1})
//4944445978444496449444486964474547474444767454648456444448886854446774444444648654448475857444494895

Some helper functions

Returns Map of occurrences of elements in a List:

countElementsInList([1, 2, 3, 1.0, 1])
//{1: {type: int, count: 2}, 1.0: {type: double, count: 1}, 2: {type: int, count: 1}, 3: {type: int, count: 1}}
countElementsInList([1, 2, 3, 1.0, 1], includeIndexes: true)
//{1: {type: int, count: 2, indexes: [0, 4]}, 1.0: {type: double, count: 1, indexes: [3]}, 2: {type: int, count: 1, indexes: [1]}, 3: {type: int, count: 1, indexes: [2]}}

Returns a List of all elements in a given range inclusive:

getRange([1,10])
//[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Returns shuffled List, cryptographically secure option:

shuffleList([1, 2, 3, 1.0, 1], cryptographicallySecure: true)
//[3, 2, 1.0, 1, 1]

printMapElements or printListElements for each element on new line

printMapElements:

List randomNums = [];
for(int i = 0; i < 100; i++){
randomNums.add(Randomness.randomInt(include: {0,1,2,3}, weights: {0:7, 1:1, 2:1, 3:1}));
}
Map results = countElementsInList(randomNums);
printMapElements(results);
/*
0: {type: int, count: 68}
1: {type: int, count: 14}
2: {type: int, count: 13}
3: {type: int, count: 5}
* */

printListElements:

printListElements([1,2,3]);
/*
1
2
3
*/
You might also like...

A Dart build script that downloads the Protobuf compiler and Dart plugin to streamline .proto to .dart compilation.

A Dart build script that downloads the Protobuf compiler and Dart plugin to streamline .proto to .dart compilation.

Oct 26, 2022

A package that lets you include a cool, nice looking and validated Password TextFormField in your app to enhance user experience. The package is fully & easily modifiable.

A package that lets you include a cool, nice looking and validated Password TextFormField in your app to enhance user experience. The package is fully & easily modifiable.

A package that lets you include a cool, nice looking and validated Password TextFormField in your app to enhance user experience. The package is fully

Jun 7, 2022

A dart package to help you parse and evaluate infix mathematical expressions into their prefix and postfix notations.

A dart package to help you parse and evaluate infix mathematical expressions into their prefix and postfix notations.

Jan 28, 2022

A Dart package to web scraping data from websites easily and faster using less code lines.

Chaleno A flutter package to webscraping data from websites This package contains a set of high-level functions that make it easy to webscrap websites

Dec 29, 2022

A dart package for decode and encode emv QR code

A dart package for decode and encode emv QR code

Nov 15, 2022

A pure dart package to apply useful rate limiting strategies on regular functions.

A pure dart package to apply useful rate limiting strategies on regular functions.

Rate limiting is a strategy for limiting an action. It puts a cap on how often someone can repeat an action within a certain timeframe. Using rate_limiter we made it easier than ever to apply these strategies on regular dart functions.

Dec 14, 2022

This package binds to Cronet's native API to expose them in Dart.

Experimental Cronet Dart bindings This package binds to Cronet's native API to expose them in Dart. This is an HTTP Client Package with almost the sam

Dec 9, 2022

Tiny Dart logging package

Tiny Dart logging package

Aug 3, 2022

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. 🎯

The Dart code generator for your package versions. There is no way to get the package version from the code in the Dart ecosystem. Installation Add bu

Dec 14, 2022
Owner
null
Dart library for unescaping HTML-encoded strings

html_unescape A Dart library for unescaping HTML-encoded strings. Supports: Named Character References ( ) 2099 of them Decimal Character Referen

Filip Hracek 36 Dec 20, 2022
With this package you can display numbers or any other text more nicely

flutter_number_animation With this package you can display numbers or any other text more nicely Preview Works with text too! How to use Add this to y

idan ben shimon 8 Jun 7, 2022
Minimal Dart wrapper to interact with Some Random Api. Easy to use, simplified and lightweight.

SRA - Some Random Api Minimal Dart wrapper to interact with Some Random Api. Easy to use, simplified and lightweight. Getting started Add the package

Yakiyo 3 Jan 4, 2023
An alternative random library for Dart.

Randt Randt library for Dart... Description Use Randt to get a random integer from a list, generate random integer in a specific range and generate ra

Bangladesh Coding Soldierz 3 Nov 21, 2021
Generate random data(string, integer, IPs etc...) using Dart.

Generate random data using Features API provides generation of: Integers in any range of numbers Strings with various characters and length Colors rep

Dinko Pehar 14 Apr 17, 2022
Flutter Cool Random User Generate 🔥🔥

Flutter Cool Random User Generate ????

Hmida 8 Sep 10, 2022
apps random

flutter_app A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if t

Tajul Aulia Muda 1 Oct 21, 2021
Random color generator for Flutter

Random color generator Pub link: https://pub.dartlang.org/packages/random_color This library will generate random colors that are visually pleasing an

Luka Knezic 56 Jun 13, 2022
null 2 Apr 17, 2022
A Dart package which supports checking if a current package is up-to-date.

pub_updater A Dart package which enables checking whether packages are up to date and supports updating them. Intended for use in CLIs for prompting u

Very Good Open Source 47 Oct 27, 2022