{"id":20364352,"url":"https://github.com/madsimple/randomness","last_synced_at":"2026-05-10T00:49:56.232Z","repository":{"id":57744981,"uuid":"518259158","full_name":"MadSimple/randomness","owner":"MadSimple","description":"Dart package for random strings and numbers, with weights","archived":false,"fork":false,"pushed_at":"2022-08-30T06:51:33.000Z","size":12,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-15T04:19:22.312Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MadSimple.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-27T00:40:24.000Z","updated_at":"2024-06-22T04:47:54.000Z","dependencies_parsed_at":"2022-08-30T11:32:02.060Z","dependency_job_id":null,"html_url":"https://github.com/MadSimple/randomness","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadSimple%2Frandomness","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadSimple%2Frandomness/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadSimple%2Frandomness/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MadSimple%2Frandomness/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MadSimple","download_url":"https://codeload.github.com/MadSimple/randomness/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241904683,"owners_count":20040021,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T00:11:23.376Z","updated_at":"2026-05-10T00:49:56.227Z","avatar_url":"https://github.com/MadSimple.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"Random number generator with or without weights.\r\n\r\nRandom bool, String, double, or other data types.\r\n\r\n## randomInt\r\n\r\nSimilar to ```Random().nextInt``` but can add a lower bound.\r\n\r\nRandom int that is \u003e= -3 and \u003c 2:\r\n```dart\r\nint i = randomInt(-3, 2);\r\n```\r\n\r\nAll functions can pass a particular ```Random``` object. ```onResult``` callback executes after the number is generated:\r\n\r\n```dart\r\nint balance = 100;\r\n\r\nint gamble = randomInt(\r\n  -100,\r\n  101,\r\n  random: Random.secure(),\r\n  onResult: (result) {\r\n    balance += result;\r\n    print('Random int: $result');\r\n    print('Current balance: $balance');\r\n  },\r\n);\r\n\r\n// Random int: -5\r\n// Current balance: 95\r\n```\r\n\r\n## randomDouble\r\n```dart\r\ndouble n = randomDouble(-3, -2);\r\n\r\ndouble n2 = randomDouble(\r\n  -3,\r\n  -2,\r\n  random: Random(1), // always the same answer since seed is given\r\n  onResult: (result) {\r\n    print(result);\r\n  },\r\n);\r\n// -2.4214987135587918\r\n```\r\n\r\n## weighted, weightedList\r\n\r\n```weighted``` returns a random element from an iterable, so elements are weighted by how many occurrences of each element:\r\n```dart\r\n// 3/4 chance of 1, 1/4 chance of 2\r\nint i = weighted([1, 1, 1, 2]);\r\n\r\n// toSet() gets rid of duplicates, so now it's 50-50\r\nint j = weighted([1, 1, 1, 2].toSet());\r\n\r\nint k = weighted(\r\n  [1, 1, 1, 2],\r\n  random: Random.secure(),\r\n  onResult: (result) =\u003e print('Result is $result'),\r\n);\r\n// Result is 1\r\n```\r\n\r\n```weightedList``` can specify length to return a List.\r\n\r\n(```onEachValue``` is called after generating each value, ```onFullResult``` is called once after generating the List.)\r\n\r\n```dart\r\nList\u003cint\u003e l = weightedList([1, 1, 2], length: 10);\r\n// Example result: [1, 1, 1, 1, 2, 1, 1, 1, 1, 2]\r\n\r\nList\u003cString\u003e l2 = weightedList(\r\n  ['hello', 'world'],\r\n  length: 5,\r\n  random: Random.secure(),\r\n  onEachValue: (index, value) =\u003e print(\"You got '$value' at index $index\"),\r\n  onFullResult: (result) =\u003e print(result),\r\n);\r\n\r\n/*\r\nYou got 'world' at index 0\r\nYou got 'hello' at index 1\r\nYou got 'world' at index 2\r\nYou got 'hello' at index 3\r\nYou got 'hello' at index 4\r\n[world, hello, world, hello, hello]\r\n*/\r\n```\r\n\r\nThe [heart](https://pub.dev/packages/heart) package can be used to generate lists to use as arguments:\r\n```dart\r\n// Same as weightedList([0, 2,..., 98, 0, 2,..., 98, 1, 3,..., 99]);\r\nList\u003cint\u003e l = weightedList(range(0, 100, 2) * 2 + range(1, 100, 2), length: 20);\r\n\r\n// [10, 35, 67, 82, 2, 9, 98, 53, 72, 98, 74, 19, 73, 4, 38, 85, 53, 34, 64, 94]\r\n\r\n```\r\n\r\n## weightedFromMap, weightedListFromMap\r\n\r\nWeights can be given as values in a Map.\r\n\r\n```weightedFromMap``` returns one value:\r\n\r\n```dart\r\n// 1/3 chance of 0, 2/3 chance of 1\r\nint i = weightedFromMap({0: 1, 1: 2});\r\n\r\n// 4/9 chance of 0, 1, or 2; 5/9 chance of 3, 4, or 5\r\nint j = weightedFromMap({randomInt(0, 3): 4, randomInt(3, 6): 5});\r\n\r\n// This will return 'hello' since 'world' has 0 weight\r\nString s = weightedFromMap(\r\n  {'hello': 1, 'world': 0},\r\n  random: Random.secure(),\r\n  onResult: (result) {\r\n    print(result);\r\n  },\r\n);\r\n```\r\n\r\n```weightedListFromMap``` can specify length to return a List:\r\n\r\n```dart\r\nint balance = 0;\r\n\r\nList\u003cint\u003e l = weightedListFromMap(\r\n  {1: 2, -1: 1},\r\n  length: 5,\r\n  random: Random.secure(),\r\n  onEachValue: (index, value) {\r\n    print('You got $value at index $index');\r\n    balance += value;\r\n  },\r\n  onFullResult: (result) =\u003e print('Final balance after $result is: $balance'),\r\n);\r\n\r\n/*\r\nYou got 1 at index 0\r\nYou got 1 at index 1\r\nYou got -1 at index 2\r\nYou got 1 at index 3\r\nYou got -1 at index 4\r\nFinal balance after [1, 1, -1, 1, -1] is: 1\r\n*/\r\n```\r\n\r\n### weightedBool\r\n\r\nWith no arguments, it gives 50/50 odds:\r\n```dart\r\nbool b = weightedBool();\r\n```\r\n\r\nWith optional arguments:\r\n```dart\r\nint balance = 100;\r\n\r\n// 3/5 chance of true\r\nbool b = weightedBool(\r\n  trueWeight: 3,\r\n  falseWeight: 2,\r\n  random: Random.secure(),\r\n  onTrue: () =\u003e balance += 100,\r\n  onFalse: () =\u003e balance -= 100,\r\n  onResult: (result) =\u003e print('Result: $result\\nBalance: $balance'),\r\n);\r\n\r\n/*\r\nResult: true\r\nBalance: 200\r\n*/\r\n```\r\n\r\n### weightedString\r\n\r\nReturns a random String from the characters in a given String. Weights are given by number of occurrences in the given String.\r\n\r\n```dart\r\n// 3/4 chance of 'á', 1/4 chance of 'é'\r\nString s = weightedString('áááé'); // 'á', default length is 1\r\nString s2 = weightedString('áéíóú', length: 10); // óáéóéáóíúí\r\n```\r\n\r\nConstants are given to help generate random Strings:\r\n```dart\r\nconst kPrintableCharacters =\r\n    ' !\"#\\$%\u0026\\'()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';\r\nconst kPrintableCharactersWithoutSpace =\r\n    '!\"#\\$%\u0026\\'()*+,-./0123456789:;\u003c=\u003e?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';\r\nconst kDigits = '0123456789';\r\nconst kUppercaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';\r\nconst kLowercaseLetters = 'abcdefghijklmnopqrstuvwxyz';\r\nconst kLetters = kUppercaseLetters + kLowercaseLetters;\r\nconst kAlphanumeric = kLetters + kDigits;\r\n```\r\n\r\n```dart\r\nString s = weightedString(\r\n  kPrintableCharacters,\r\n  length: 5,\r\n  random: Random.secure(),\r\n  onEachCharacter: (index, c) =\u003e print('$c at index $index'),\r\n  onFullResult: (result) =\u003e print(result),\r\n);\r\n\r\n/*\r\nx at index 0\r\nw at index 1\r\n- at index 2\r\nl at index 3\r\nj at index 4\r\nxw-lj\r\n*/\r\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadsimple%2Frandomness","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmadsimple%2Frandomness","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmadsimple%2Frandomness/lists"}