{"id":27401284,"url":"https://github.com/edjcase/motoko_random","last_synced_at":"2026-01-23T02:19:31.843Z","repository":{"id":203179322,"uuid":"708976300","full_name":"edjCase/motoko_random","owner":"edjCase","description":"Random function in Motoko","archived":false,"fork":false,"pushed_at":"2025-09-06T21:47:52.000Z","size":36,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-09-06T23:31:34.062Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Motoko","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edjCase.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-10-23T19:06:09.000Z","updated_at":"2025-09-06T21:47:56.000Z","dependencies_parsed_at":"2025-04-14T03:54:46.255Z","dependency_job_id":"1821c37e-2faf-43f5-a075-03ad78c1f26b","html_url":"https://github.com/edjCase/motoko_random","commit_stats":null,"previous_names":["edjcase/motoko_random"],"tags_count":1,"template":false,"template_full_name":"edjCase/motoko-library-template","purl":"pkg:github/edjCase/motoko_random","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_random","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_random/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_random/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_random/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edjCase","download_url":"https://codeload.github.com/edjCase/motoko_random/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_random/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28677998,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T01:00:35.747Z","status":"online","status_checked_at":"2026-01-23T02:00:08.296Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-04-14T03:54:39.984Z","updated_at":"2026-01-23T02:19:31.837Z","avatar_url":"https://github.com/edjCase.png","language":"Motoko","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Random Number Generator Library\n\nThis library provides two implementations of random number generators:\n\n1. Pseudo Random Number Generator\n2. Extension methods to Random.Random from core library\n\n## Pseudo Random Number Generator\n\nThis generator provides a pseudo-random number generator with various methods for generating random numbers, selecting random elements, and performing weighted selections.\n\n### Usage\n\nImport the module to use this library:\n\n```motoko\nimport PseudoRandom \"mo:pseudo-random/PseudoRandom\";\n```\n\n### Types\n\n#### PseudoRandomGenerator\n\nThe main type provided by this library, which includes all the methods for random number generation and selection.\n\n### Functions\n\n#### fromBlob\n\nCreates a new PseudoRandomGenerator from a Blob.\n\n```motoko\npublic func fromBlob(blob : Blob) : PseudoRandomGenerator\n```\n\n#### fromSeed\n\nCreates a new PseudoRandomGenerator from a 32-bit seed.\n\n```motoko\npublic func fromSeed(seed : Nat32) : PseudoRandomGenerator\n```\n\n#### getCurrentSeed\n\nReturns the current seed of the generator.\n\n```motoko\ngetCurrentSeed : () -\u003e Nat32\n```\n\n#### nextInt\n\nGenerates a random integer within the specified range (exclusive).\n\n```motoko\nnextInt : (min : Int, max : Int) -\u003e Int\n```\n\n#### nextNat\n\nGenerates a random natural number within the specified range (exclusive).\n\n```motoko\nnextNat : (min : Nat, max : Nat) -\u003e Nat\n```\n\n#### nextFloat\n\nGenerates a random floating-point number within the specified range.\n\n```motoko\nnextFloat : (min : Float, max : Float) -\u003e Float\n```\n\n#### nextCoin\n\nSimulates a coin flip, returning a random boolean value.\n\n```motoko\nnextCoin : () -\u003e Bool\n```\n\n#### nextRatio\n\nReturns a boolean based on the specified ratio of true outcomes to total outcomes.\n\n```motoko\nnextRatio : (trueCount : Nat, totalCount : Nat) -\u003e Bool\n```\n\n#### nextListElement\n\nSelects a random element from the given list.\n\n```motoko\nnextListElement : \u003cT\u003e(list : List.List\u003cT\u003e) -\u003e T\n```\n\n#### nextArrayElement\n\nSelects a random element from the given array.\n\n```motoko\nnextArrayElement : \u003cT\u003e(array : [T]) -\u003e T\n```\n\n#### nextArrayElementWeighted\n\nSelects a random element from the given array of tuples, where each tuple contains an element and its weight.\n\n```motoko\nnextArrayElementWeighted : \u003cT\u003e(array : [(T, Float)]) -\u003e T\n```\n\n#### nextArrayElementWeightedFunc\n\nSelects a random element from the given array using a provided weight function.\n\n```motoko\nnextArrayElementWeightedFunc : \u003cT\u003e(array : [T], weightFunc : (T) -\u003e Float) -\u003e T\n```\n\n#### shuffleList\n\nShuffles the elements of the given list in place.\n\n```motoko\nshuffleList : \u003cT\u003e(list : List.List\u003cT\u003e) -\u003e ()\n```\n\n### Examples\n\nHere are some examples of how to use the Pseudo Random Number Generator:\n\n```motoko\n// Create a generator from a seed value\nlet prng = PseudoRandomX.fromSeed(0);\n\nlet randomInt = prng.nextInt(1, 10);\nDebug.print(\"Random integer between 1 and 10 (exclusive): \" # Int.toText(randomInt));\n\nlet randomCoin = prng.nextCoin();\nDebug.print(\"Random coin flip: \" # Bool.toText(randomCoin));\n\n\nlet randomFloat = prng.nextFloat(0.0, 1.0);\nDebug.print(\"Random float between 0.0 and 1.0 (exclusive): \" # Float.toText(randomFloat));\n\nlet list = List.fromArray\u003cNat\u003e([1, 2, 3, 4, 5]);\nprng.shuffleList(list);\nDebug.print(\"Shuffled list: \" # debug_show (List.toArray(list)));\n\n// Select a random element from an array\nlet array = [1, 2, 3, 4, 5];\nlet randomElement = prng.nextArrayElement(array);\nDebug.print(\"Random element from array: \" # Int.toText(randomElement));\n\n```\n\nNote: This pseudo-random number generator is deterministic and should not be used for cryptographic purposes or in situations where true randomness is required.\n\n## Extension methods to Random.Random in core library\n\nSome helper functions for Random.Random that aren't included in the core library\n\n### Usage\n\nImport the module to use this library:\n\n```motoko\nimport Random \"mo:core/Random\";\nimport RandomX \"mo:xtended-random/RandomX\";\n```\n\n### Functions\n\n#### nextRatio\n\nReturns a boolean based on the specified ratio of true outcomes to total outcomes.\n\n```motoko\nnextRatio : (random : Random.Random, trueCount : Nat, totalCount : Nat) -\u003e Bool\n```\n\n#### nextFloat\n\nGenerates a random floating-point number within the specified range.\n\n```motoko\nnextFloat : (random : Random.Random, min : Float, max : Float) -\u003e Float\n```\n\n#### nextListElement\n\nSelects a random element from the given list.\n\n```motoko\nnextListElement : \u003cT\u003e(random : Random.Random, list : List.List\u003cT\u003e) -\u003e T\n```\n\n#### nextArrayElement\n\nSelects a random element from the given array.\n\n```motoko\nnextArrayElement : \u003cT\u003e(random : Random.Random, array : [T]) -\u003e T\n```\n\n#### nextArrayElementWeighted\n\nSelects a random element from the given array of tuples, where each tuple contains an element and its weight.\n\n```motoko\nnextArrayElementWeighted : \u003cT\u003e(random : Random.Random, array : [(T, Float)]) -\u003e T\n```\n\n#### nextArrayElementWeightedFunc\n\nSelects a random element from the given array using a provided weight function.\n\n```motoko\nnextArrayElementWeightedFunc : \u003cT\u003e(random : Random.Random, array : [T], weightFunc : (T) -\u003e Float) -\u003e T\n```\n\n#### shuffleList\n\nShuffles the elements of the given list in place.\n\n```motoko\nshuffleList : \u003cT\u003e(random : Random.Random, list : List.List\u003cT\u003e) -\u003e ()\n```\n\n### Examples\n\nHere are some examples of how to use the Finite Random Number Generator:\n\n```motoko\nlet list = List.fromArray\u003cNat\u003e([1, 2, 3, 4, 5]);\nlet random = Random.seed(123);\n\n// Shuffle a list\nRandomX.shuffleList(random, list);\nDebug.print(\"Shuffled list: \" # debug_show (List.toArray(list)));\n\n// Generate a random float\nlet randomFloat = RandomX.nextFloat(random, 0.0, 1.0);\nDebug.print(\"Random float between 0.0 and 1.0: \" # Float.toText(randomFloat));\n\n// Select a random element from a list\nlet fruits = List.fromArray\u003cText\u003e([\"apple\", \"banana\", \"orange\"]);\nlet randomFruit = RandomX.nextListElement(random, fruits);\nDebug.print(\"Random fruit: \" # randomFruit);\n\n// Select a random element from an array\nlet numbers = [1, 2, 3, 4, 5];\nlet randomNumber = RandomX.nextArrayElement(random, numbers);\nDebug.print(\"Random number: \" # Int.toText(randomNumber));\n\n// Weighted selection\nlet weightedItems = [(\"rare\", 0.1), (\"uncommon\", 0.3), (\"common\", 0.6)];\nlet randomItem = RandomX.nextArrayElementWeighted(random, weightedItems);\nDebug.print(\"Random weighted item: \" # randomItem);\n\n// Weighted selection with function\nlet items = [1, 2, 3, 4, 5];\nlet weightFunc = func (n : Nat) : Float { Float.fromInt(n) };\nlet randomWeightedNumber = RandomX.nextArrayElementWeightedFunc(random, items, weightFunc);\nDebug.print(\"Random weighted number: \" # Int.toText(randomWeightedNumber));\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedjcase%2Fmotoko_random","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedjcase%2Fmotoko_random","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedjcase%2Fmotoko_random/lists"}