{"id":20817391,"url":"https://github.com/golang-infrastructure/go-shuffle","last_synced_at":"2025-05-07T13:41:03.331Z","repository":{"id":64535919,"uuid":"557907360","full_name":"golang-infrastructure/go-shuffle","owner":"golang-infrastructure","description":"提供几种洗牌算法的Go实现","archived":false,"fork":false,"pushed_at":"2023-02-28T16:34:03.000Z","size":17,"stargazers_count":4,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-31T10:39:00.905Z","etag":null,"topics":["go","golang","shuffle"],"latest_commit_sha":null,"homepage":"","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/golang-infrastructure.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}},"created_at":"2022-10-26T14:26:26.000Z","updated_at":"2024-08-19T16:07:38.000Z","dependencies_parsed_at":"2024-11-17T21:41:48.095Z","dependency_job_id":"bdda0ed6-509a-4f19-a57d-a94a401bfcca","html_url":"https://github.com/golang-infrastructure/go-shuffle","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-infrastructure%2Fgo-shuffle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-infrastructure%2Fgo-shuffle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-infrastructure%2Fgo-shuffle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/golang-infrastructure%2Fgo-shuffle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/golang-infrastructure","download_url":"https://codeload.github.com/golang-infrastructure/go-shuffle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252888926,"owners_count":21820094,"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":["go","golang","shuffle"],"created_at":"2024-11-17T21:41:42.826Z","updated_at":"2025-05-07T13:41:03.298Z","avatar_url":"https://github.com/golang-infrastructure.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[English Document](./README_English.md)\n\n[中文文档](./README.md)\n\n# 洗牌算法（Shuffle Algorithm）\n\n# 一、支持的洗牌算法\n\n洗牌算法的定义：为有限集合生成随机排序的算法。\n\n目前支持的洗牌算法： \n\n- Fisher–Yates-Knuth\n- Scatology\n\n# 二、安装\n\n```bash\ngo get -u github.com/golang-infrastructure/go-shuffle\n```\n\n# 三、API代码示例\n\n## 3.1 对切片shuffle\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/golang-infrastructure/go-shuffle\"\n)\n\nfunc main() {\n\n\t// 对切片中的元素shuffle\n\tslice := []int{1, 2, 3, 4, 5}\n\tshuffle.Shuffle(slice)\n\tfmt.Println(slice)\n\t// Output:\n\t// [5 1 2 3 4]\n\n}\n```\n\n## 3.2 对矩阵shuffle\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/golang-infrastructure/go-shuffle\"\n)\n\nfunc main() {\n\n\t// 对二维矩阵中的元素shuffle\n\tmatrix := [][]int{\n\t\t{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},\n\t\t{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},\n\t\t{21, 22, 23, 24, 25, 26, 27, 28, 29, 30},\n\t\t{31, 32, 33, 34, 35, 36, 37, 38, 39, 40},\n\t}\n\t// 注意可能会返回错误，比如二维数组每行长度不一致则无法shuffle\n\terr := shuffle.ShuffleMatrix(matrix)\n\tif err != nil {\n\t\tfmt.Println(\"Shuffle matrix failed: \" + err.Error())\n\t\treturn\n\t}\n\tfmt.Println(matrix)\n\t// Output:\n\t// [[11 40 6 23 15 28 4 7 37 21] [29 26 33 5 35 13 22 32 19 34] [31 30 36 20 2 10 24 39 9 27] [16 8 18 14 1 17 38 12 25 3]]\n\n}\n```\n\n# 四、Fisher–Yates-Knuth洗牌算法\n\n假设现在有一个数组：\n\n```\n[1, 2, 3, 4, 5]\n```\n\n从最右边的坐标`len(slice)-1`开始作为`right_index`，每次从`[0, right_index]`随机选择一个下标，将选中的下标的值与`right_index`交换，并将`right_index`减一往左偏移。\n\n代码示例：\n\n```go\n// 使用自己独立的随机数生成器，与其它的调用区分开\nvar standaloneRand = rand.New(rand.NewSource(time.Now().Unix()))\n\n// FisherYatesKnuthShuffle Fisher–Yates-Knuth Shuffle或 算法对一维数组洗牌，O(n)\nfunc FisherYatesKnuthShuffle[T any](slice []T) {\n\tfor index := len(slice) - 1; index \u003e 0; index-- {\n\t\tchooseIndex := standaloneRand.Intn(index + 1)\n\t\tslice[chooseIndex], slice[index] = slice[index], slice[chooseIndex]\n\t}\n}\n```\n\n我们对上面的算法扩展一下，很容易就能得到矩阵的shuffle算法，将矩阵的每一行看做是拼接起来的一维数组，则将对矩阵进行shuffle的算法转换为了对切片shufle的算法，而对切片进行shuffle我们已经实现过了，API代码示例：\n\n```go\n// FisherYatesShuffleMatrix Fisher–Yates-Knuth shuffle算法对矩阵洗牌\nfunc FisherYatesShuffleMatrix[T any](matrix [][]T) error {\n\n\t// 参数检查\n\tif err := check(matrix); err != nil {\n\t\treturn err\n\t}\n\n\trow, col := len(matrix), len(matrix[0])\n\tfor index := row*col - 1; index \u003e 0; index-- {\n\t\tchooseIndex := standaloneRand.Intn(index + 1)\n\t\tmatrix[index/col][index%col], matrix[chooseIndex/col][chooseIndex%col] = matrix[chooseIndex/col][chooseIndex%col], matrix[index/col][index%col]\n\t}\n\n\treturn nil\n}\n\n// 需要保证传入的二维数据是一个矩阵，否则后面可能会越界panic\nfunc check[T any](matrix [][]T) error {\n\tfor i := 1; i \u003c len(matrix); i++ {\n\t\tif len(matrix[i]) != len(matrix[i-1]) {\n\t\t\treturn ErrMatrixUnavailable\n\t\t}\n\t}\n\treturn nil\n}\n```\n\n# 五、Scatology算法\n\n就是在Fisher–Yates-Knuth的基础上随机选择的时候不再选择最右边的`[0,right_index)`，不再展开详解。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgolang-infrastructure%2Fgo-shuffle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgolang-infrastructure%2Fgo-shuffle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgolang-infrastructure%2Fgo-shuffle/lists"}