{"id":19094485,"url":"https://github.com/risto-stevcev/pure-random","last_synced_at":"2025-04-30T12:46:36.378Z","repository":{"id":60973774,"uuid":"53234654","full_name":"Risto-Stevcev/pure-random","owner":"Risto-Stevcev","description":":sparkles: A purely functional random number generator","archived":false,"fork":false,"pushed_at":"2016-03-06T04:37:07.000Z","size":10,"stargazers_count":27,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-15T20:22:49.472Z","etag":null,"topics":["csprng","fp","functional","generator","number","prng","pure","purely","random","xorshift"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/pure-random","language":"JavaScript","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/Risto-Stevcev.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}},"created_at":"2016-03-06T02:57:26.000Z","updated_at":"2024-11-14T03:08:13.000Z","dependencies_parsed_at":"2022-10-07T18:58:49.814Z","dependency_job_id":null,"html_url":"https://github.com/Risto-Stevcev/pure-random","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/Risto-Stevcev%2Fpure-random","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Risto-Stevcev%2Fpure-random/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Risto-Stevcev%2Fpure-random/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Risto-Stevcev%2Fpure-random/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Risto-Stevcev","download_url":"https://codeload.github.com/Risto-Stevcev/pure-random/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251703241,"owners_count":21630205,"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":["csprng","fp","functional","generator","number","prng","pure","purely","random","xorshift"],"created_at":"2024-11-09T03:29:52.018Z","updated_at":"2025-04-30T12:46:36.356Z","avatar_url":"https://github.com/Risto-Stevcev.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pure-random\n\n[![Build Status](https://travis-ci.org/Risto-Stevcev/pure-random.svg)](https://travis-ci.org/Risto-Stevcev/pure-random)\n\nA purely functional random number generator. It implements the `xorshift` algorithm. Xorshift RNGs which are a class of PRNGs that are extremely fast on modern computers and outperm other PRNGs in performance and BigCrush stress testing. \n\n\n## Usage\n\n```js\n\u003e const rnd = require('pure-random')\n\u003e rnd.genCsSeed()\n[ 2628481196, 1837393298, 2892949381, 1706851469 ]\n\u003e rnd.random(rnd.genCsSeed(), 0, 10)\nRight(8)\n```\n\nPassing in the same seed with the same params produces the same number:\n\n```js\n\u003e const seed = rnd.genCsSeed()\n\u003e seed\n[ 426141121, 700962946, 3633913687, 2605998810 ]\n\u003e rnd.random(seed, 0, 10)\nRight(7)\n\u003e rnd.random(seed, 0, 10)\nRight(7)\n\u003e rnd.random(seed, 0, 10)\nRight(7)\n```\n\n\n## Reference \n\n#### `genSeed`\n\n```hs\n:: () -\u003e [Uint32]\n```\nUses `Date.now` to generate a non-cryptographically secure seed. It is more performant than `genCsSeed`, and appropriate for most cases. The seed is an array of four `Uint32` numbers. Since there is no `Uint32` in javascript yet, integers within the range **[0, 4294967295]** are returned.\n\n#### `genCsSeed`\n\n```hs\n:: () -\u003e [Uint32]\n```\n\nUses `crypto.randomBytes` to generate a cryptographically secure seed. It is useful for creating an `initialization vector` for cryptography, or in any situation where you need the generated random number to have significant entropy. The seed is an array of four `Uint32` numbers. Since there is no `Uint32` in javascript yet, integers within the range **[0, 4294967295]** are returned.\n\n#### `randUint`\n\n```hs\n:: [UInt32] -\u003e UInt32\n```\nTakes a `seed` and returns a random `UInt32` value, which in javascript is an integer within the range **[0, 4294967295]**. This function is actually the javascript `xorshift` implementation, so it is extremely fast. The other methods call this method under the hood. \n\n#### `randFloat`\n\n```hs\n :: [UInt32] -\u003e Int -\u003e Int -\u003e Either Error Float\n```\n\nTakes a `seed` and a `min` and `max` range. It returns an `Either` value that is `Left Error` if the function was called with invalid parameters, or a `Right Float` within the specified range (inclusive). \n\n#### `random`\n\n```hs\n :: [UInt32] -\u003e Int -\u003e Int -\u003e Either Error Int\n```\n\nTakes a `seed` and a `min` and `max` range. It returns an `Either` value that is `Left Error` if the function was called with invalid parameters, or a `Right Int` within the specified range (inclusive).\n\n\n\n## Faq\n\n#### Why wasn't Math.random used?\n\n`Math.random` is non-deterministic and doesn't have take a seed value, which means that you cannot return results consistently, which is against the purely functional paradigm. Note that this library has an option for more powerful entropy than `Math.random` by using the `genCsSeed` option.\n\n#### What about xorshift\\* or xorshift+?\n\nThe javascript number type uses a [double-precision 64-bit binary format IEEE 754 value](https://en.wikipedia.org/wiki/IEEE_floating_point), which is a number between **-(2\u003csup\u003e53\u003c/sup\u003e - 1)** and **2\u003csup\u003e53\u003c/sup\u003e - 1**. The xorshift\\* and xorshift+ libraries rely on `Uint64` types, which javascript does not support. If a developer erroneously decides to represent `Uint64` using the native number type, then any `Uint64` type will lose precision on the upper end because a javascript number will only go up to **2\u003csup\u003e53\u003c/sup\u003e - 1** but a range of **0** to **2\u003csup\u003e64\u003c/sup\u003e - 1** is needed for `Uint64`.\n\nIn general, it would be too complicated to implement these other algorithms because you would have to jump through a lot of hoops with javascript's lack of a type system, and with very little gain. These improvements to xorshift are only slightly more performant.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fristo-stevcev%2Fpure-random","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fristo-stevcev%2Fpure-random","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fristo-stevcev%2Fpure-random/lists"}