{"id":13663041,"url":"https://github.com/khadzhynov/RandomUtils","last_synced_at":"2025-04-25T13:31:18.135Z","repository":{"id":47538567,"uuid":"263863758","full_name":"khadzhynov/RandomUtils","owner":"khadzhynov","description":"Set of utils for make work with random selections more comfortable. Includes Randomizer and Weighted List.","archived":false,"fork":false,"pushed_at":"2020-06-09T11:49:31.000Z","size":443,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-10T19:33:28.806Z","etag":null,"topics":["random","unity","utils","weights"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khadzhynov.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}},"created_at":"2020-05-14T08:53:54.000Z","updated_at":"2023-01-22T00:26:17.000Z","dependencies_parsed_at":"2022-09-10T04:23:06.035Z","dependency_job_id":null,"html_url":"https://github.com/khadzhynov/RandomUtils","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/khadzhynov%2FRandomUtils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khadzhynov%2FRandomUtils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khadzhynov%2FRandomUtils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khadzhynov%2FRandomUtils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khadzhynov","download_url":"https://codeload.github.com/khadzhynov/RandomUtils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250824926,"owners_count":21493366,"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":["random","unity","utils","weights"],"created_at":"2024-08-02T05:02:15.790Z","updated_at":"2025-04-25T13:31:17.100Z","avatar_url":"https://github.com/khadzhynov.png","language":"C#","funding_links":["https://www.patreon.com/user?u=26313020\u0026fan_landing=true"],"categories":["C\\#"],"sub_categories":[],"readme":"# RandomUtils\nSet of utils for make work with random selections more comfortable. \n\nIncludes Randomizer and Weighted List.\n\n### Asset store ###\n https://assetstore.unity.com/packages/slug/169229\n\n## Features ##\n#### Randomizer ####\n- Non-repetitions random.\n- Flat-distributed random.\n#### Weighted List ####\n- Allow to set weights for list items, and then select randomly by weight.\n- Inherins IList\u003cT\u003e and IReadOnlyList\u003cT\u003e interface - you can use it as usual List\u003cT\u003e.\n- Property drawer included to easy setup in inspector.\n\n## Randomizer API ##\n\nTo use Randomizer, you need to create and store its instance:\n```\n  _randomizer = new Randomizer(_prefabsList.Count);\n```\nIt needs to be initialized with amount of items to select from.\nRandomizer designed to work with arrays and lists, so, its only returns inreger indexes, limited in range from 0 to initialized amount.\n\nTo get indexes without repetitions, use next:\n```\n  int index = _randomizer.SelectNoRepeat();\n```\n\nTo get indexes flat-distributed:\n```\n  int index = _randomizer.SelectFlatDistributed();\n```\n\nBoth functions returns integer number in range from 0 to amount value, which constructor was initialized with.\n\n## WeightedList API ##\n\n```WeightedList\u003cT\u003e ``` inherits standard C# interfaces ```IList\u003cT\u003e``` and ```IReadOnlyList\u003cT\u003e```. \nInternally, it consists form usual ```List\u003cT\u003e```, and list of assotiated weights (```List\u003cfloat\u003e```). \nSo, you can use it in your code exactly like ```List\u003cT\u003e```, except you have to add weights, when its necessary:\n\nConstructor (also parameter-less constructor available):\n```WeightedList(List\u003cT\u003e objects, List\u003cfloat\u003e weights)```\n  \n- ```int IndexOf(float weight)``` - get first index of item with given weight.\n- ```int IndexOf(T item)``` - get first index of item.\n- ```void Insert(int index, T item)``` - inserts item at index with 0 weight.\n- ```void Insert(int index, T item, float weight)``` - inserts item at index with given weight.\n- ```void Add(T item, float weight)```- adds item to the end of list with given weight.\n- ``` void Add(T item)``` - adds item to the end of list with 0 weight.\n\nTo keep usage safe, Weighted List implements read-only interface:\n\n```\npublic interface IReadOnlyWeightedList\u003cT\u003e\n    {\n        T GetRandomByWeight();\n        float GetWeightAtIndex(int index);\n        float GetTotalWeight();\n        float GetNormalizedWeightAtIndex(int index);\n    }\n\n```\nLets describe it:\n- ```T GetRandomByWeight()``` - itd a main routine to use: it randomly selects an item from list by its weight.\n- ```float GetWeightAtIndex(int index)``` - allows you to know the weight at particular index.\n- ```float GetTotalWeight()``` - returns the sum of all weights.\n- ``` float GetNormalizedWeightAtIndex(int index)``` - Normalized weights means the weight value, proportional to absolute value, but when the total weights sum equals 1. For example, we have two items, weigths are 2 and 6. Normalized weights for these items are 0.25 and 0.75.\n\nAdditionally, ```WeightedList\u003cT\u003e``` provides next methods:\n\n- ```void SetWeightAtIndex(int index, float weight)``` - sets weight for item with given index.\n- ```void Normalize()``` - normalizes all weights (total weights sum will be 1).\n- ```void SetWeightOf(T item, float weight)``` - sets weight for particular item (first occurance).\n\n## Weighted List property drawer ##\nTo make it comfortable to use Weighted List in unity, custom property drawer provided.\n\nUsage:\n1. Inherit concrete implementation of generic WeightedList (dont forget the Serializable attribute):\n```\n[Serializable] public class WeightedListOfPrefabs : WeightedList\u003cCustomPrefab\u003e { }\n```\n2. Inherit property drawer and mark it with attribute:\n```\n[CustomPropertyDrawer(typeof(WeightedListOfPrefabs))]\npublic class MyPropertyDrawer : WeightedListPropertyDrawer { }\n```\nProvided property drawer supports any basic type, and any type, inherited from UnityEngine.Object.\nPlain c# classes supported as read-only (but no limits to use plain classes in code).\n\n## Demo video ##\n\nhttps://youtu.be/0Irz4ShEJbs \n\n## Contact ##\nMail:\nbryarey@gmail.com\n\nSkype:\nm.khadzhynov\n\nSupport me on Patreon:\nhttps://www.patreon.com/user?u=26313020\u0026fan_landing=true\n\nSubscribe on Games Garden channel on YouTube:\nhttps://www.youtube.com/channel/UCH6WybnFgT199Kkd6vm7-Lg\n\nLinkedIn:\nhttps://www.linkedin.com/in/mykhaylo-khadzhynov-15635915/\n\nGames Garden web site:\nhttps://www.gamesgarden.net/\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhadzhynov%2FRandomUtils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhadzhynov%2FRandomUtils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhadzhynov%2FRandomUtils/lists"}