{"id":16572804,"url":"https://github.com/lemire/fastshuffleexperiments","last_synced_at":"2025-03-21T12:31:15.357Z","repository":{"id":66054856,"uuid":"70163141","full_name":"lemire/FastShuffleExperiments","owner":"lemire","description":"How fast can we shuffle values?","archived":false,"fork":false,"pushed_at":"2024-03-12T16:58:41.000Z","size":70,"stargazers_count":37,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T01:11:22.506Z","etag":null,"topics":["algorithm","performance","random-string","shuffle"],"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/lemire.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":"2016-10-06T14:43:28.000Z","updated_at":"2024-07-19T02:14:06.000Z","dependencies_parsed_at":"2024-10-28T10:24:21.890Z","dependency_job_id":"b20509bb-d991-485e-b3c8-0e57e3b14d36","html_url":"https://github.com/lemire/FastShuffleExperiments","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/lemire%2FFastShuffleExperiments","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2FFastShuffleExperiments/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2FFastShuffleExperiments/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lemire%2FFastShuffleExperiments/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lemire","download_url":"https://codeload.github.com/lemire/FastShuffleExperiments/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244799321,"owners_count":20512234,"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":["algorithm","performance","random-string","shuffle"],"created_at":"2024-10-11T21:28:37.215Z","updated_at":"2025-03-21T12:31:15.031Z","avatar_url":"https://github.com/lemire.png","language":"C++","readme":"# FastShuffleExperiments\nHow fast can we shuffle values?\n\nThe [nearly divisionless technique](https://arxiv.org/abs/1805.10941) benchmarked in this repository has been widely adopted:\n  * by the [standard C++ Linux library (GNU libstdc++)](https://gcc.gnu.org/git/?p=gcc.git;a=blobdiff;f=libstdc%2B%2B-v3/include/bits/uniform_int_dist.h;h=ecb8574864aee10b9ea164379fffef27c7bdb0df;hp=6e1e3d5fc5fe8f7f22e62a85b35dc8bfa4743372;hb=98c37d3bacbb2f8bbbe56ed53a9547d3be01b66b;hpb=6ce2cb116af6e0965ff0dd69e7fd1925cf5dc68c) to accelerate the std::uniform_int_distribution function,\n * by [Google's Abseil C++ Common Libraries](https://github.com/abseil/abseil-cpp),\n * by [the Swift standard library](https://github.com/apple/swift/pull/25286),\n * by the [Go language](https://github.com/golang/go/commit/a2dfe5d278eae0864397a046a8206342a426d2bd),\n * by the  [Julia language](https://github.com/JuliaLang/julia/pull/29240),\n * by the [Zig language](https://github.com/ziglang/zig/blob/98183e47436699f6e5eab200061c46eec342806e/std/rand.zig#L74-L118),\n * and by [Numpy](https://github.com/numpy/numpy/blob/6420e7f528a6c42422966544e453bdb2805ff620/numpy/random/generator.pyx) (Python).\n \n # Requirements\n\nWe assume that you have a Linux-like system.\n\nTo reproduce the figures from the ACM Transactions on Modeling and Computer Simulation article, please go to the TOMACS_RCR subdirectory and consult the README.md file for instructions. \n\nUsage:\n\n```\ncd cpp\nmake\n./shuffle\n```\n# Why is it faster?\n\nThe core idea is that you can achieve faster shuffling by avoiding division. We \ngenerate division when trying to map random numbers to an interval. Let us compare\nthese three functions:\n\n```C++\n// returns value in [0,s)\n// random64 is a function returning random 64-bit words\nuint64_t openbsd(uint64_t s, uint64_t (*random64)(void)) {\n  uint64_t t = (-s) % s;\n  uint64_t x;\n  do {\n    x = random64();\n  } while (x \u003c t);\n  return x % s;\n}\n\n// returns value in [0,s)\n// random64 is a function returning random 64-bit words\nuint64_t java(uint64_t s, uint64_t (*random64)(void)) {\n  uint64_t x = random64();\n  uint64_t r = x % s;\n  while (x - r \u003e\n         UINT64_MAX - s + 1) { \n    x = random64();\n    r = x % s;\n  }\n  return r;\n}\n\n// returns value in [0,s)\n// random64 is a function returning random 64-bit words\nuint64_t nearlydivisionless(uint64_t s, uint64_t (*random64)(void)) {\n  uint64_t x = random64();\n  __uint128_t m = (__uint128_t) x * (__uint128_t) s;\n  uint64_t l = (uint64_t) m;\n  if (l \u003c s) {\n   uint64_t t = -s % s;\n    while (l \u003c t) {\n      x = random64();\n      m = (__uint128_t) x * (__uint128_t) s;\n      l = (uint64_t) m;\n    }\n  }\n  return m \u003e\u003e 64; \n}\n```\n\nThe first one always generates two divisions, the second one usually generates one division whereas the last one rarely requires a division at all.\n\n## Reference\n\n* Daniel Lemire, [Fast Random Integer Generation in an Interval](https://arxiv.org/abs/1805.10941), ACM Transactions on Modeling and Computer Simulation Volume 29 Issue 1, February 2019\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Ffastshuffleexperiments","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flemire%2Ffastshuffleexperiments","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flemire%2Ffastshuffleexperiments/lists"}