{"id":24628332,"url":"https://github.com/chainrand/chainrand-cpp","last_synced_at":"2026-07-15T02:31:58.239Z","repository":{"id":119216523,"uuid":"432701148","full_name":"chainrand/chainrand-cpp","owner":"chainrand","description":"C++ header-only library for verifiable hybrid-chain RNG. ","archived":false,"fork":false,"pushed_at":"2021-12-04T06:50:52.000Z","size":9,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-19T10:02:57.310Z","etag":null,"topics":["aes-intrinsics","chainlink","chainlink-hackathon-2021","chainlink-vrf","cpp","cryptographic-random-generator","random-number-generators"],"latest_commit_sha":null,"homepage":"https://chainrand.io","language":"C++","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/chainrand.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-11-28T12:05:17.000Z","updated_at":"2024-01-13T20:15:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"0f040b5f-810f-40c3-85fa-68951b8305aa","html_url":"https://github.com/chainrand/chainrand-cpp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chainrand/chainrand-cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chainrand","download_url":"https://codeload.github.com/chainrand/chainrand-cpp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-cpp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":35488230,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-07-15T02:00:06.706Z","response_time":131,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["aes-intrinsics","chainlink","chainlink-hackathon-2021","chainlink-vrf","cpp","cryptographic-random-generator","random-number-generators"],"created_at":"2025-01-25T05:29:35.669Z","updated_at":"2026-07-15T02:31:58.232Z","avatar_url":"https://github.com/chainrand.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chainrand-cpp — Verifiable hybrid-chain RNG.\n\nMany applications require off-chain generation of random numbers for efficiency, security, etc.\n\nThis class allows you to generate a stream of deterministic, high-quality,  \ncryptographically secure random numbers.\n\nBy seeding it with a Chainlink VRF result that is requested **only once for the project**,  \nit can be used to demonstrate that the random numbers are **not cherry-picked**.\n\n# Requirements\n\nC++98 and above compiler. \n\n# Installation\n\nJust copy and paste and include `include/chainrand.h` into your project.\n\n## Usage\n\n```c++\nchainrand::CRNG crng(\"base10(\u003cRNG_VRF_RESULT\u003e)\" \"\u003cRNG_SEED_KEY\u003e\");\n// prints 10 determinstic random numbers between [0, 1)\nfor (int i = 0; i \u003c 10; ++i) {\n    std::cout \u003c\u003c crng() \u003c\u003c \"\\n\";\n}\n```\n\nCompile with optimizations `-march=native` flag to enable usage of AES instructions. \n\n# Reproducibility\n\nCurrent and future versions of this library will generate the same stream of random numbers from the same seed.\n\n# Functions\n\n## (constructor)\n\n```c++\ntemplate \u003cclass Str\u003e CRNG(Str seed);\n```\n\nCreates an instance of the crng initialized with the `seed`.\n\n**Parameters:**\n\n- `seed` If empty, defaults to the empty string `\"\"`.\n\n**Example:**\n\n```c++\nchainrand::CRNG crng(\"base10(\u003cRNG_VRF_RESULT\u003e)\" + \"\u003cRNG_SEED_KEY\u003e\");\n```\n\n## nextUint\n\n```c++\ntemplate \u003cclass T\u003e T nextUint();\nuint8_t nextUint8();\nuint16_t nextUint16();\nuint32_t nextUint32();\nuint64_t nextUint64();\n```\n\n**Returns:**\nReturns a single uniform random number within [0, (1\u003c\u003c(sizeof(T)*8))-1].\n\n## nextDouble / random / operator()\n\n```c++\ndouble nextDouble();\ndouble operator() ();\ndouble random();\n```\n\nReturns a single uniform random number within [0,1).\nThe numbers are in multiples of 2**-53.\n\n**Parameters:**\nnone\n\n**Returns:**\nReturns a single uniform random number within [0,1).\n\n\n## nextFloat\n\n```c++\nfloat nextFloat();\n```\n\nReturns a single uniform random number within [0,1).\nThe numbers are in multiples of 2**-24.\n\n**Parameters:**\nnone\n\n**Returns:**\nReturns a single uniform random number within [0,1).\n\n## randrange\n\n```c++\nint64_t randrange(int64_t start, int64_t stop, int64_t step);\nint64_t randrange(int64_t start, int64_t stop);\nint64_t randrange(int64_t stop);\n```\n\nReturns a random integer uniformly distributed in [start, stop).  \nThe integers are spaced with intervals of |step|.\n\n**Parameters:**\n\n- `start` The start of the range. (optional, default=`0`)\n- `stop` The end of the range.\n- `step` The interval step. (optional, default=`1`)\n\n**Returns:**\n\nA random integer uniformly distributed in [start, stop).\n\n**Examples:**\n\n```c++\nint64_t r;\nr = crng.randrange(3); // returns a random number in {0,1,2}\nr = crng.randrange(-3); // returns a random number in {0,-1,-2}\nr = crng.randrange(0, 6, 2); // returns a random number in {0,2,4}\nr = crng.randrange(5, 0, 1); // returns a random number in {5,4,3,2,1}\nr = crng.randrange(5, -5, -2); // returns a random number in {5,3,1,-1,-3}\n```\n\n## randint\n\n```c++\nint64_t randint(int64_t start, int64_t stop);\nint64_t randint(int64_t stop);\n```\n\nReturns a random integer uniformly distributed in [start, stop].  \nThe integers are spaced with intervals of |step|.\n\n**Parameters:**\n\n- `start` The start of the range. (optional, default=`0`)\n- `stop` The end of the range.\n\n**Returns:**\n\nA random integer uniformly distributed in [start, stop].\n\n**Examples:**\n\n```c++\nint64_t r;\nr = crng.randint(3); // returns a random number in {0,1,2,3}\nr = crng.randint(-3); // returns a random number in {0,-1,-2,-3}\nr = crng.randint(-3, 1); // returns a random number in {-3,-2,-1,0,1}\nr = crng.randint(3, -1); // returns a random number in {3,2,1,0,-1}\n```\n\n## choose (iterator)\n\n```c++\ntemplate \u003cclass ChoicePointer, class PopulationIterator, class WeightsIterator\u003e\nbool choose(ChoicePointer choicePointer,\n            PopulationIterator populationBegin,\n            PopulationIterator populationEnd,\n            WeightsIterator weightsBegin,\n            WeightsIterator weightsEnd);\n```\n\nChooses a random element from the population.\n\n`ChoicePointer`, `PopulationIterator`, `WeightsIterator`  \ncan be plain old pointer types, or pointer-like classes. \n\nIf weights is not provided, every element of population will be equally weighted.\n\nIf weights are provided,  \nthe first `min(populationEnd - populationBegin, weightsEnd - weightsBegin)`  \nelements of the population will be considered.\n\nIf the sum of the weights is less than or equal to zero,  \nevery element of population will be equally weighted.\n\n**Parameters:**\n\n- `choicePointer[out]`  A pointer to the choosen element.\n- `populationBegin` An iterator to the start of the population.\n- `populationEnd` An iterator to the end of the population.\n- `weightsBegin` An iterator to the start of the population. (optional)\n- `weightsEnd` An iterator to the end of the population. (optional)\n\n**Returns:**\n\nWhether an element has been choosen. \n\n**Examples:**\n\n```c++\nstd::vector\u003cint\u003e population;\npopulation.push_back(1);\npopulation.push_back(2);\npopulation.push_back(3);\nint choice;\nif (crng.choose(choice, population.begin(), population.end())) {\n    std::cout \u003c\u003c choice \u003c\u003c \"\\n\";\n}\n\nstd::vector\u003cdouble\u003e weights;\nweights.push_back(10);\nweights.push_back(1);\nweights.push_back(0.1);\nif (crng.choose(choice, population.begin(), population.end(), \n                weights.begin(), weights.end())) {\n    std::cout \u003c\u003c choice \u003c\u003c \"\\n\";\n}\n```\n\n## choose (vector)\n\n```c++\ntemplate \u003cclass ChoicePointer, class Population, class Weights\u003e\nbool choose(ChoicePointer choicePointer,\n            const Population \u0026population,\n            const Weights \u0026weights);\n```\n\nChooses a random element from the population.\n\n`Population`, `Weight` can be `std::vector`,   \nor classes with the `operator[]` and `size()` methods.\n\nIf weights is not provided, every element of population will be equally weighted.\n\nIf weights are provided,  \nthe first `min(population.size(), weights.size())`  \nelements of the population will be considered.\n\nIf the sum of the weights is less than or equal to zero,  \nevery element of population will be equally weighted.\n\n**Parameters:**\n\n- `choicePointer[out]`  A pointer to the choosen element.\n- `population` A vector/array-like container of elements.\n- `weights` A vector/array-like container of weights. (optional)\n\n**Returns:**\n\nWhether an element has been choosen. \n\n**Examples:**\n\n```c++\nstd::vector\u003cint\u003e population;\npopulation.push_back(1);\npopulation.push_back(2);\npopulation.push_back(3);\nint choice;\nif (crng.choose(choice, population)) {\n    std::cout \u003c\u003c choice \u003c\u003c \"\\n\";\n}\n\nstd::vector\u003cdouble\u003e weights;\nweights.push_back(10);\nweights.push_back(1);\nweights.push_back(0.1);\nif (crng.choose(choice, population, weights)) {\n    std::cout \u003c\u003c choice \u003c\u003c \"\\n\";\n}\n```\n\n## sample (iterator)\n\n```c++\ntemplate \u003cclass CollectedIterator, class PopulationIterator, class WeightsIterator\u003e\nsize_t sample(CollectedIterator collectedBegin,\n              PopulationIterator populationBegin,\n              PopulationIterator populationEnd,\n              size_t k,\n              WeightsIterator weightsBegin,\n              WeightsIterator weightsEnd);\n```\n\nChooses `k` random elements from the population **without** replacement.\n\n`CollectedIterator`, `PopulationIterator`, `WeightsIterator`  \ncan be plain old pointer types, or pointer-like classes. \n\nIf `k` is more than the length of the population, only `k` elements will be returned.\n\nIf weights is not provided, every element of population will be equally weighted.\n\nIf weights are provided,  \nthe first `min(populationEnd - populationBegin, weightsEnd - weightsBegin)`  \nelements of the population will be considered.\n\nIf the sum of the weights is less than or equal to zero,  \nevery element of population will be equally weighted.\n\n**Parameters:**\n\n- `collectedBegin[out]` An iterator to the collected results.\n- `populationBegin` An iterator to the start of the population.\n- `populationEnd` An iterator to the end of the population.\n- `k` The number of elements to choose. (optional, default=`1`)\n- `weightsBegin` An iterator to the start of the weights. (optional)\n- `weightsEnd` An iterator to the end of the weights. (optional)\n\n**Returns:**\n\nThe number of elements choosen.\n\n**Examples:**\n\n```c++\nconst int k = 2;\nstd::vector\u003cint\u003e population, collected(k);\npopulation.push_back(1);\npopulation.push_back(2);\npopulation.push_back(3);\n\ncrng.sample(collected.begin(), population.begin(), population.end(), k);\n\nfor (int i = 0; i \u003c k; ++i)\n    std::cout \u003c\u003c collected[i] \u003c\u003c \" \";\nstd::cout \u003c\u003c \"\\n\";\n\nstd::vector\u003cdouble\u003e weights;\nweights.push_back(10);\nweights.push_back(1);\nweights.push_back(0.1);\ncrng.sample(collected.begin(), population.begin(), population.end(), k, \n            weights.begin(), weights.end());\n\nfor (int i = 0; i \u003c k; ++i)\n    std::cout \u003c\u003c collected[i] \u003c\u003c \" \";\nstd::cout \u003c\u003c \"\\n\";\n```\n\n## sample (vector)\n\n```c++\ntemplate \u003cclass Collected, class Population, class Weights\u003e\nsize_t sample(Collected \u0026collected,\n        const Population \u0026population,\n        size_t k,\n        const Weights \u0026weights);\n```\n\nChooses `k` random elements from the population **without** replacement.\n\nThe `Population` and `Weights` can be `std::vector`,   \nor classes with the `operator[]` and `size()` methods.\n\nIf `k` is more than the length of the population, only `k` elements will be returned.\n\nIf weights is not provided, every element of population will be equally weighted.\n\nIf weights are provided,  \nthe first `min(populationEnd - populationBegin, weightsEnd - weightsBegin)`  \nelements of the population will be considered.\n\nIf the sum of the weights is less than or equal to zero,  \nevery element of population will be equally weighted.\n\n**Parameters:**\n\n- `collected[out]` A vector/array-like container of elements.\n- `population` A vector/array-like container of elements.\n- `k` The number of elements to choose. (optional, default=`1`)\n- `weights` A vector/array-like container of weights. (optional)\n\n**Returns:**\n\nThe number of elements choosen.\n\n**Examples:**\n\n```c++\nconst int k = 2;\nstd::vector\u003cint\u003e population, collected(k);\npopulation.push_back(1);\npopulation.push_back(2);\npopulation.push_back(3);\n\ncrng.sample(collected.begin(), population.begin(), population.end(), k);\n\nfor (int i = 0; i \u003c k; ++i)\n    std::cout \u003c\u003c collected[i] \u003c\u003c \" \";\nstd::cout \u003c\u003c \"\\n\";\n\nstd::vector\u003cdouble\u003e weights;\nweights.push_back(10);\nweights.push_back(1);\nweights.push_back(0.1);\ncrng.sample(collected.begin(), population.begin(), population.end(), k, \n            weights.begin(), weights.end());\n\nfor (int i = 0; i \u003c k; ++i)\n    std::cout \u003c\u003c collected[i] \u003c\u003c \" \";\nstd::cout \u003c\u003c \"\\n\";\n```\n\n\n## shuffle (iterator)\n\n```c++\ntemplate \u003cclass RandomAccessIterator\u003e\nvoid shuffle(RandomAccessIterator begin, RandomAccessIterator end);\n```\n\nShuffles the elements in-place.\n\n`RandomAccessIterator` can be plain old pointer type, or pointer-like class. \n\n**Parameters:**\n\n- `begin[in/out]` An iterator to the start of the sequence.\n- `end[in/out]` An iterator to the end of the sequence.\n\n\n## shuffle (vector)\n\n```c++\ntemplate \u003cclass Vector\u003e \nvoid shuffle(Vector \u0026v);\n```\n\nShuffles the elements in-place.\n\n`Vector` can be `std::vector`,  \nor class with the `operator[]` and `size()` methods.\n\n**Parameters:**\n\n- `v[in/out]` A vector/array-like container of elements.\n\n\n## gauss\n\n```c++\ndouble gauss(double mu, double sigma);\n```\n\nNormal distribution, also called the Gaussian distribution. \n\n**Parameters:**\n\n- `mu`  The mean. (optional, default=`0.0`)\n- `sigma` The standard deviation. (optional, default=`1.0`)\n\n**Returns:**\n\nA random number from the Gaussian distribution.\n\n# License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchainrand%2Fchainrand-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchainrand%2Fchainrand-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchainrand%2Fchainrand-cpp/lists"}