{"id":15886856,"url":"https://github.com/kalloc/go-rand-bench","last_synced_at":"2025-06-19T04:04:12.617Z","repository":{"id":209541161,"uuid":"724343141","full_name":"kalloc/go-rand-bench","owner":"kalloc","description":null,"archived":false,"fork":false,"pushed_at":"2023-11-27T22:27:48.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-19T04:03:34.273Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kalloc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-11-27T22:12:38.000Z","updated_at":"2023-11-27T22:21:04.000Z","dependencies_parsed_at":"2023-11-27T23:44:00.838Z","dependency_job_id":null,"html_url":"https://github.com/kalloc/go-rand-bench","commit_stats":null,"previous_names":["kalloc/go-rand-bench"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kalloc/go-rand-bench","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kalloc%2Fgo-rand-bench","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kalloc%2Fgo-rand-bench/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kalloc%2Fgo-rand-bench/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kalloc%2Fgo-rand-bench/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kalloc","download_url":"https://codeload.github.com/kalloc/go-rand-bench/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kalloc%2Fgo-rand-bench/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260684101,"owners_count":23046098,"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-10-06T05:41:50.752Z","updated_at":"2025-06-19T04:04:07.596Z","avatar_url":"https://github.com/kalloc.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n## Code\n\n```go\npackage main\n\nimport (\n    \"math/rand\"\n    \"fmt\"\n)\n\n\nfunc generateRandomString(length int) string {\n    const alphanum = \"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n    const size = len(alphanum)\n    b := make([]byte, length)\n    for i := range b {\n        b[i] = alphanum[rand.Intn(size)]\n    }\n    return string(b)\n}\n\nfunc generateRandomString2(length int) string {\n    b := make([]byte, length)\n    rand.Read(b)\n    return fmt.Sprintf(\"%x\", b)\n}\n\nfunc main() {\n    fmt.Println(generateRandomString(10))\n    fmt.Println(generateRandomString2(5))\n}\n```\n\n\n## Output\n\n```shell\nSquVJQ5yYD\n41100e791b\n```\n\n## Explanation\n\nThis is a small example showing how `fmt` can be expensive.\nPlease note that `math/rand` is not a good source for generating passwords and keys, use `crypto/rand` instead.\n\n## Benchmark\n\n```shell\ngo test -bench . -benchmem\n```\n\n```shell\n\ngoos: linux\ngoarch: amd64\npkg: example.com/m\ncpu: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz\nBenchmarkGenerateRandomString-12     \t 8636812\t       129.6 ns/op\t      16 B/op\t       1 allocs/op\nBenchmarkGenerateRandomString2-12    \t 8805774\t       146.9 ns/op\t      40 B/op\t       3 allocs/op\nPASS\nok  \texample.com/m\t2.698s\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkalloc%2Fgo-rand-bench","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkalloc%2Fgo-rand-bench","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkalloc%2Fgo-rand-bench/lists"}