rand 5.0.1 copy "rand: ^5.0.1" to clipboard
rand: ^5.0.1 copied to clipboard

Seedable test-data generator: names, emails, IPs, UUIDv4/v7, ULID, hex, slugs, digits, semver, lorem, CSS colors, geo, weighted samples, CSPRNG tokens. Pure Dart. Agent skill.

example/main.dart

// coverage:ignore-file
// Demo of every Rand.* public method with formatted CLI output.
// ignore_for_file: avoid_print

import 'dart:math';

import 'package:rand/rand.dart';

void main() {
  print('\x1B[36m');
  print('╔══════════════════════════════════════════════════════════════╗');
  print('║                       Rand Package Demo                      ║');
  print('╚══════════════════════════════════════════════════════════════╝');
  print('\x1B[0m');

  _section('Numbers');
  print('  Integer [0, 100):    ${Rand.integer(max: 100)}');
  print('  Integer [50, 100):   ${Rand.integer(min: 50, max: 100)}');
  print('  Float [0, 1):        ${Rand.float().toStringAsFixed(4)}');
  print('  Boolean (50%):       ${Rand.boolean()}');
  print('  Boolean (90% true):  ${Rand.boolean(trueChance: 90)}');

  _section('Geo');
  print('  Latitude:   ${Rand.latitude()}');
  print('  Longitude:  ${Rand.longitude()}');
  final point = Rand.geoPoint();
  print('  Geo Point:  (${point.lat}, ${point.lng})');
  print('  City:       ${Rand.city()}');

  _section('Identity');
  print('  First Name: ${Rand.firstName()}');
  print('  Last Name:  ${Rand.lastName()}');
  print('  Full Name:  ${Rand.fullName()}');
  print('  Alias:      ${Rand.alias()}');

  _section('Text');
  print('  Word:      ${Rand.word()}');
  print('  Words(5):  ${Rand.words(count: 5)}');
  print('  Sentence:  ${Rand.sentence()}');
  print('');
  print('  Paragraph (3 sentences):');
  print('  ${_indent(Rand.paragraph(count: 3))}');

  _section('Networking');
  print('  Email:    ${Rand.email()}');
  print('  Email:    ${Rand.email(domain: 'mycompany.io')}');
  print('  IPv4:     ${Rand.ipv4()}');
  print('  IPv6:     ${Rand.ipv6()}');
  print('  MAC:      ${Rand.mac()}');
  print('  Hex (40): ${Rand.hex(length: 40)}');
  print('  Slug:     ${Rand.slug()}');
  print('  Semver:   ${Rand.semver()}');
  print('  Digits:   ${Rand.digits()}');

  _section('Cryptographic');
  print('  Nonce (32):    ${Rand.nonce(length: 32)}');
  print('  Password:      ${Rand.password()}');
  print('  Password (no symbols): ${Rand.password(symbols: false)}');
  print('  Bytes (8):     ${Rand.bytes(length: 8)}');
  print('  Base64 (32):   ${Rand.base64(byteLength: 32)}');

  _section('Time');
  print('  DateTime:             ${Rand.dateTime()}');
  print(
    '  DateTime (2020-2025): '
    '${Rand.dateTime(min: DateTime.utc(2020), max: DateTime.utc(2025))}',
  );
  final duration = Rand.duration(
    min: const Duration(days: 1),
    max: const Duration(days: 30),
  );
  print('  Duration (1-30 days): $duration');

  _section('Colors');
  final color = Rand.color();
  final dark = Rand.colorDark();
  final light = Rand.colorLight();
  print('  Random:     ${color.name} (${_hex(color.argb)})');
  print('  Dark only:  ${dark.name} (${_hex(dark.argb)})');
  print('  Light only: ${light.name} (${_hex(light.argb)})');

  _section('Collections');
  final fruits = {'apple', 'orange', 'lemon', 'grape', 'kiwi'};
  final scores = {'Alice': 95, 'Bob': 87, 'Charlie': 92};
  print('  Element:    ${Rand.element(fruits)} from $fruits');
  print('  Subset(3):  ${Rand.subset(fruits, count: 3)}');
  print('  Shuffled:   ${Rand.shuffled(fruits.toList())}');
  print('  Map Key:    ${Rand.mapKey(scores)} from ${scores.keys}');
  print('  Map Value:  ${Rand.mapValue(scores)} from ${scores.values}');
  print('  Enum:       ${Rand.enumValue(_DemoLevel.values)}');

  _section('Sampling');
  final positions = Rand.sample(
    ['GK', 'DEF', 'MID', 'FWD'],
    count: 11,
    weights: [10, 40, 40, 10],
  );
  print('  Football team (weighted):');
  print('  ${positions.join(', ')}');
  print('');
  final rarityDemo = Rand.sample(
    ['Legendary', 'Rare', 'Common'],
    count: 20,
    weights: [1, 10, 100],
  );
  final legendary = rarityDemo.where((e) => e == 'Legendary').length;
  final rare = rarityDemo.where((e) => e == 'Rare').length;
  final common = rarityDemo.where((e) => e == 'Common').length;
  print(
    '  Loot box (20 items): $legendary legendary, $rare rare, $common common',
  );

  _section('IDs & seeding');
  print('  UUID v4:            ${Rand.uuidV4()}');
  print('  UUID v7:            ${Rand.uuidV7(time: DateTime.now())}');
  print('  ULID:               ${Rand.ulid(time: DateTime.now())}');
  print('  RandGen(Random(1)): ${RandGen(Random(1)).fullName()}');
  print('  withSeed(7, email): ${Rand.withSeed(7, Rand.email)}');

  _section('Nullable');
  print('  nullable("value", 50%): ${Rand.nullable("value") ?? "null"}');
  print(
    '  nullable("value", 90%): '
    '${Rand.nullable("value", nullChance: 90) ?? "null"}',
  );

  print('\n\x1B[36m═════════════════════════════════════════════════\x1B[0m\n');
}

void _section(String title) {
  print('\n\x1B[33m▸ $title\x1B[0m');
}

String _indent(String text, [int spaces = 2]) {
  return text.split('\n').map((line) => '${' ' * spaces}$line').join('\n');
}

String _hex(int color) =>
    '#${(color & 0xFFFFFF).toRadixString(16).padLeft(6, '0').toUpperCase()}';

enum _DemoLevel { info, warn, error }
5
likes
160
points
179
downloads

Documentation

API reference

Publisher

verified publishermehmetesen.com

Weekly Downloads

Seedable test-data generator: names, emails, IPs, UUIDv4/v7, ULID, hex, slugs, digits, semver, lorem, CSS colors, geo, weighted samples, CSPRNG tokens. Pure Dart. Agent skill.

Repository (GitHub)
View/report issues
Contributing

Topics

#random #fixtures #fake-data #mock #testing

License

MIT (license)

More

Packages that depend on rand