{"id":41145781,"url":"https://github.com/func25/mafu","last_synced_at":"2026-01-22T18:55:46.082Z","repository":{"id":41549280,"uuid":"502508008","full_name":"func25/mafu","owner":"func25","description":null,"archived":false,"fork":false,"pushed_at":"2022-09-30T03:04:38.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-07-27T22:07:28.329Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","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/func25.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":"2022-06-12T03:19:06.000Z","updated_at":"2022-06-12T03:20:06.000Z","dependencies_parsed_at":"2022-07-07T23:40:37.700Z","dependency_job_id":null,"html_url":"https://github.com/func25/mafu","commit_stats":null,"previous_names":[],"tags_count":5,"template":null,"template_full_name":null,"purl":"pkg:github/func25/mafu","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/func25%2Fmafu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/func25%2Fmafu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/func25%2Fmafu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/func25%2Fmafu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/func25","download_url":"https://codeload.github.com/func25/mafu/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/func25%2Fmafu/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28668643,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T17:07:18.858Z","status":"ssl_error","status_checked_at":"2026-01-22T17:05:02.040Z","response_time":144,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":"2026-01-22T18:55:45.336Z","updated_at":"2026-01-22T18:55:46.068Z","avatar_url":"https://github.com/func25.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mafu\n\nMafu is a set of simple math utils that I use for my project, which make writing code more convenient (with support of generic 1.18):\n\n- Max, min, random\n- Bit processing for enum flag\n- Randomly select an element according to the ratio (or the expect rate)\n- ...\n\n## Content:\n\n- [Installation](#installation)\n- [Samples](#samples)\n  - [Find min, max](#find-min-max)\n  - [Enum flag](#enum-flag)\n  - [Random number](#random-number)\n  - [Random with rarities](#random-with-rarities)\n  - [Random with expect](#random-with-expect)\n\n## Installation\n\n1. You first need Go installed (version 1.18+ is required):\n\n```sh\n$ go get -u github.com/func25/mafu\n```\n\n2. Import it in your code:\n\n```go\nimport \"github.com/func25/mafu\"\n```\n\n## Samples\n\n### Find min, max:\n\nSupport ellipsis (...) with all primitive number types: int (int, uint, int32,...), float (float32, float64):\n\n```go\nmafu.Min(58, 39, 24, 68, 34)            // 24\nmafu.Min(58.5, 39.1, 24.4, 68.67, 34.3) // 24.4\nmafu.Max(58.5, 39.1, 24.4, 68.67, 34.3) // 68.67\n```\n\n### Enum flag:\n\nthis flag is used when you want a variable that can store multiple states\n\n```go\ntype MultiColor int\n\nconst (\n\tColorBlack MultiColor = 1 \u003c\u003c iota\n\tColorRed\n\tColorGreen\n\tColorBlue\n)\n\nfunc main() {\n\tmix := ColorBlack | ColorGreen | ColorBlue\n\tfmt.Println(mix) // 13 = 1101\n\n\tmafu.FlagHas(mix, ColorGreen)       // true\n\tmix = mafu.FlagOff(mix, ColorGreen) // 9 = 1001\n\tmafu.FlagHas(mix, ColorGreen)       // false\n}\n```\n\n### Random number\n\nAlso support all int types (int, uint, int32,...) and float types (float32, float64):\n\n```go\nfunc main() {\n\tmafu.Rand0ToInt(100)        // 6\n\tmafu.RandFloat(0.0, 100.0)  // 38.9706330749286\n}\n```\n\n### Random with rarities:\n\nYou may want to use RandRateUnits or RandRarities (return index) to random element in an array with rarities:\n\n```go\nfunc main() {\n\tquantity := map[string]int{}\n\n\tfor i := 0; i \u003c 10000; i++ {\n\t\tres, _ := mafu.RandRateUnits([]mafu.RateUnit[string]{\n\t\t\t{Key: \"a\", Rate: 0.1},\n\t\t\t{Key: \"ab\", Rate: 0.1},\n\t\t\t{Key: \"abc\", Rate: 0.1},\n\t\t\t{Key: \"abcd\", Rate: 0.1},\n\t\t\t{Key: \"abcde\", Rate: 0.6},\n\t\t})\n\t\tquantity[res.Key]++\n\t}\n\n\tfmt.Println(quantity)\n}\n// result: map[a:1048 ab:962 abc:993 abcd:1049 abcde:5948]\n\nfunc main() {\n\tquantity := map[int]int{}\n\tfor i := 0; i \u003c 10000; i++ {\n\t\tres, _ := mafu.RandRarities([]float64{0.1, 0.1, 0.1, 0.1, 0.6})\n\t\tquantity[res]++\n\t}\n\n\tfmt.Println(quantity)\n}\n// result: map[0:1074 1:993 2:1069 3:1086 4:5778]\n\n```\n\n### Random with expect\n\nIf you want more precise result, use `RandExpect` algorithm:\n\n```go\nfunc main() {\n\tshowed := map[string]uint{}\n\tfor i := 0; i \u003c 10000; i++ {\n\t\tres, _ := mafu.RandExpect([]mafu.ExpectUnit[string]{\n\t\t\t{Key: \"a\", Expect: 0.1},\n\t\t\t{Key: \"ab\", Expect: 0.1},\n\t\t\t{Key: \"abc\", Expect: 0.1},\n\t\t\t{Key: \"abcd\", Expect: 0.1},\n\t\t\t{Key: \"abcde\", Expect: 0.6},\n\t\t}, showed)\n\n\t\tshowed[res.Key]++\n\t}\n\n\tfmt.Println(showed)\n}\n// result: map[a:1000 ab:1000 abc:1000 abcd:1000 abcde:6000]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffunc25%2Fmafu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffunc25%2Fmafu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffunc25%2Fmafu/lists"}