{"id":24628303,"url":"https://github.com/chainrand/chainrand-js","last_synced_at":"2025-06-17T07:33:22.178Z","repository":{"id":57196719,"uuid":"423521116","full_name":"chainrand/chainrand-js","owner":"chainrand","description":"Javascript library for verifiable hybrid-chain RNG.","archived":false,"fork":false,"pushed_at":"2021-12-04T06:50:40.000Z","size":15,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-21T18:28:17.307Z","etag":null,"topics":["chainlink","chainlink-hackathon-2021","chainlink-vrf","cryptographic-random-generator","es5-javascript","javascript","nodejs","random","random-number-generators"],"latest_commit_sha":null,"homepage":"https://chainrand.io","language":"JavaScript","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}},"created_at":"2021-11-01T15:40:36.000Z","updated_at":"2022-08-08T14:22:31.000Z","dependencies_parsed_at":"2022-09-15T23:21:26.109Z","dependency_job_id":null,"html_url":"https://github.com/chainrand/chainrand-js","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chainrand/chainrand-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chainrand","download_url":"https://codeload.github.com/chainrand/chainrand-js/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chainrand%2Fchainrand-js/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260314247,"owners_count":22990549,"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":["chainlink","chainlink-hackathon-2021","chainlink-vrf","cryptographic-random-generator","es5-javascript","javascript","nodejs","random","random-number-generators"],"created_at":"2025-01-25T05:18:31.154Z","updated_at":"2025-06-17T07:33:22.131Z","avatar_url":"https://github.com/chainrand.png","language":"JavaScript","readme":"# Chainrand-js — 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\nA Javascript ES5 envrionment: NodeJS, most web browsers, even older ones.\n\n# Installation\n\n**Browser / CDN:**\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/chainrand/chainrand.min.js\"\u003e\u003c/script\u003e\n```\n\n**NPM:**\n\n```bash\nnpm i chainrand\n```\n\nOr you can clone/download this GitHub repository.\n\n## Usage\n\n```jsx\nvar rng = chainrand.CRNG(\"base10(\u003cRNG_VRF_RESULT\u003e)\" + \"\u003cRNG_SEED_KEY\u003e\")\n// prints 10 determinstic random numbers between [0, 1)\nfor (var i = 0; i \u003c 10; ++i) {\n    console.log(rng())\n}\n```\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```jsx\nchainrand.CRNG(seed)\n```\n\nCreates an instance of the crng initialized with the `seed`.\n\n**Parameters:**\n\n- `seed: String` If empty, defaults to the empty string `\"\"`.\n\n**Example:**\n\n```jsx\nvar crng = chainrand.CRNG(\"base10(\u003cRNG_VRF_RESULT\u003e)\" + \"\u003cRNG_SEED_KEY\u003e\")\n```\n\n## random\n\n```jsx\ncrng.random(): Number\n```\n\nAlias for `crng()`.\nReturns a random number uniformly distributed in [0, 1).  \nThe numbers are in multiples of `2**-53`.\n\n**Parameters:**\nnone\n\n**Returns:**\nA random number uniformly distributed in [0, 1).\n\n## randrange\n\n```jsx\ncrng.randrange(start, stop[, step]): Integer\ncrng.randrange(stop): Integer\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: Integer` The start of the range. (optional, default=`0`)\n- `stop: Integer` The end of the range.\n- `step: Integer` The interval step. (optional, default=`1`)\n\n**Returns:**\n\nA random integer uniformly distributed in [start, stop).\n\n**Examples:**\n\n```jsx\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```jsx\ncrng.randint(start, stop): Integer\ncrng.randint(stop): Integer\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: Integer` The start of the range. (optional, default=`0`)\n- `stop: Integer` The end of the range.\n\n**Returns:**\n\nA random integer uniformly distributed in [start, stop].\n\n**Examples:**\n\n```jsx\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## choice\n\n```jsx\ncrng.choice(population[, weights]): Array\n```\n\nReturns a random element from the population.\n\nIf weights is not provided, every element of population will be equally weighted.\n\nIf weights is a non-empty array and is of different length to population,  \nonly the first `Math.min(population.length, weights.length)` elements of population are sampled.\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- `population: Array`  The population.\n- `weights: Array\u003cNumber\u003e` The weights of the population. (optional)\n\n**Returns:**\n\nA random element in the population.\n\n**Examples:**\n\n```jsx\n/* returns a random number in {1,2,3} */\nr = crng.choice([1,2,3]) \n\n/* returns a random number in {1,2,3}\n   with the weights {1:10, 2:1, 3:0.1} */\nr = crng.choice([1,2,3], [10,1,0.1]) \n```\n\n## sample\n\n```jsx\ncrng.sample(population, k=1[, weights]): Array\n```\n\nReturns `k` random elements from the population, sampling **without** replacement.\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 is a non-empty array and is of different length to population,  \nonly the first `Math.min(population.length, weights.length)` elements of population are sampled.\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- `population: Array`  The population.\n- `k: Integer` The number of elements to choose.\n- `weights: Array\u003cNumber\u003e` The weights of the population. (optional)\n\n**Returns:**\n\nAn array of `k` random elements from the population.\n\n**Examples:**\n\n```jsx\n/* returns an array of 1 random element from {1,2,3} */\nr = crng.sample([1,2,3]) \n\n/* returns an array of 2 random elements from {1,2,3} */\nr = crng.sample([1,2,3], 2) \n\n/* returns an array of 2 random elements from {1,2,3}\n   with the weights {1:10, 2:1, 3:0.1} */\nr = crng.sample([1,2,3], 2, [10,1,0.1]) \n```\n\n## shuffle\n\n```jsx\ncrng.shuffle(population)\n```\n\nShuffles the array in-place.\n\n**Parameters:**\n\n- `population: Array`  The population.\n\n**Returns:** \n\nThe shuffled array. \n\n## gauss\n\n```jsx\ncrng.gauss(mu=0.0, sigma=1.0): Number\n```\n\nNormal distribution, also called the Gaussian distribution. \n\n**Parameters:**\n\n- `mu: Number`  The mean. (optional, default=`0.0`)\n- `sigma: Number` The standard deviation. (optional, default=`1.0`)\n\n**Returns:**\n\nA random number from the Gaussian distribution.\n\n# License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchainrand%2Fchainrand-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchainrand%2Fchainrand-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchainrand%2Fchainrand-js/lists"}