├── .eslintrc.json ├── .github ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── ci.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bower.json ├── package.json └── src └── Effect ├── Random.js └── Random.purs /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "ecmaVersion": 6, 4 | "sourceType": "module" 5 | }, 6 | "extends": "eslint:recommended", 7 | "rules": { 8 | "strict": [2, "global"], 9 | "block-scoped-var": 2, 10 | "consistent-return": 2, 11 | "eqeqeq": [2, "smart"], 12 | "guard-for-in": 2, 13 | "no-caller": 2, 14 | "no-extend-native": 2, 15 | "no-loop-func": 2, 16 | "no-new": 2, 17 | "no-param-reassign": 2, 18 | "no-return-assign": 2, 19 | "no-unused-expressions": 2, 20 | "no-use-before-define": 2, 21 | "radix": [2, "always"], 22 | "indent": [2, 2], 23 | "quotes": [2, "double"], 24 | "semi": [2, "always"] 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | **Description of the change** 2 | 3 | Clearly and concisely describe the purpose of the pull request. If this PR relates to an existing issue or change proposal, please link to it. Include any other background context that would help reviewers understand the motivation for this PR. 4 | 5 | --- 6 | 7 | **Checklist:** 8 | 9 | - [ ] Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)") 10 | - [ ] Linked any existing issues or proposals that this pull request should close 11 | - [ ] Updated or added relevant documentation 12 | - [ ] Added a test for the contribution (if applicable) 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: [master] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | 15 | - uses: purescript-contrib/setup-purescript@main 16 | with: 17 | purescript: "unstable" 18 | 19 | - uses: actions/setup-node@v2 20 | with: 21 | node-version: "14.x" 22 | 23 | - name: Install dependencies 24 | run: | 25 | npm install -g bower 26 | npm install 27 | bower install --production 28 | 29 | - name: Build source 30 | run: npm run-script build 31 | 32 | - name: Run tests 33 | run: | 34 | bower install 35 | npm run-script test --if-present 36 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.* 2 | !/.gitignore 3 | !/.eslintrc.json 4 | !/.github/ 5 | /bower_components/ 6 | /node_modules/ 7 | /output/ 8 | package-lock.json 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 4 | 5 | ## [Unreleased] 6 | 7 | Breaking changes: 8 | 9 | New features: 10 | 11 | Bugfixes: 12 | 13 | Other improvements: 14 | 15 | ## [v6.0.0](https://github.com/purescript/purescript-random/releases/tag/v6.0.0) - 2022-04-27 16 | 17 | Breaking changes: 18 | - Migrate FFI to ES modules (#29 by @JordanMartinez) 19 | 20 | New features: 21 | 22 | Bugfixes: 23 | 24 | Other improvements: 25 | 26 | ## [v5.0.0](https://github.com/purescript/purescript-random/releases/tag/v5.0.0) - 2021-02-26 27 | 28 | Breaking changes: 29 | - Added support for PureScript 0.14 and dropped support for all previous versions 30 | 31 | New features: 32 | 33 | Bugfixes: 34 | 35 | Other improvements: 36 | - Migrated CI to GitHub Actions and updated installation instructions to use Spago (#25) 37 | - Added a changelog and pull request template (#26, #27) 38 | 39 | ## [v4.0.0](https://github.com/purescript/purescript-random/releases/tag/v4.0.0) - 2018-05-23 40 | 41 | - Updated for PureScript 0.12 42 | 43 | ## [v3.0.0](https://github.com/purescript/purescript-random/releases/tag/v3.0.0) - 2017-03-26 44 | 45 | - Updated for PureScript 0.11 46 | 47 | ## [v2.0.0](https://github.com/purescript/purescript-random/releases/tag/v2.0.0) - 2016-10-03 48 | 49 | - Updated dependencies 50 | 51 | ## [v1.0.0](https://github.com/purescript/purescript-random/releases/tag/v1.0.0) - 2016-06-01 52 | 53 | This release is intended for the PureScript 0.9.1 compiler and newer. **Note**: The v1.0.0 tag is not meant to indicate the library is “finished”, the core libraries are all being bumped to this for the 0.9 compiler release so as to use semver more correctly. 54 | 55 | - Fixed error for operator section usage 56 | 57 | ## [v0.2.3](https://github.com/purescript/purescript-random/releases/tag/v0.2.3) - 2015-11-20 58 | 59 | - Removed unused import (@tfausak) 60 | 61 | ## [v0.2.2](https://github.com/purescript/purescript-random/releases/tag/v0.2.2) - 2015-08-02 62 | 63 | - Fixed overflows in randomInt (#9, @hdgarrood) 64 | 65 | ## [v0.2.1](https://github.com/purescript/purescript-random/releases/tag/v0.2.1) - 2015-07-23 66 | 67 | - Fixed behaviour of `randomInt` and `randomRange` (@sharkdp) 68 | 69 | ## [v0.2.0](https://github.com/purescript/purescript-random/releases/tag/v0.2.0) - 2015-06-30 70 | 71 | - This release works with versions 0.7.\* of the PureScript compiler. It will not work with older versions. If you are using an older version, you should require an older, compatible version of this library. 72 | 73 | ## [v0.1.3](https://github.com/purescript/purescript-random/releases/tag/v0.1.3) - 2015-03-19 74 | 75 | - Updated docs 76 | 77 | ## [v0.1.2](https://github.com/purescript/purescript-random/releases/tag/v0.1.2) - 2015-02-07 78 | 79 | - Added `randomInt` and `randomRange` (@jutaro) 80 | 81 | ## [v0.1.1](https://github.com/purescript/purescript-random/releases/tag/v0.1.1) - 2014-04-27 82 | 83 | - Updated ignored files 84 | 85 | ## [v0.1.0](https://github.com/purescript/purescript-random/releases/tag/v0.1.0) - 2014-04-27 86 | 87 | - Initial release 88 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2018 PureScript 2 | 3 | Redistribution and use in source and binary forms, with or without modification, 4 | are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this 7 | list of conditions and the following disclaimer. 8 | 9 | 2. Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation and/or 11 | other materials provided with the distribution. 12 | 13 | 3. Neither the name of the copyright holder nor the names of its contributors 14 | may be used to endorse or promote products derived from this software without 15 | specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 18 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 21 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 24 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # purescript-random 2 | 3 | [![Latest release](http://img.shields.io/github/release/purescript/purescript-random.svg)](https://github.com/purescript/purescript-random/releases) 4 | [![Build status](https://github.com/purescript/purescript-random/workflows/CI/badge.svg?branch=master)](https://github.com/purescript/purescript-random/actions?query=workflow%3ACI+branch%3Amaster) 5 | [![Pursuit](https://pursuit.purescript.org/packages/purescript-random/badge)](https://pursuit.purescript.org/packages/purescript-random) 6 | 7 | Random value generation. 8 | 9 | ## Installation 10 | 11 | ``` 12 | spago install random 13 | ``` 14 | 15 | ## Documentation 16 | 17 | Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-random). 18 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "purescript-random", 3 | "homepage": "https://github.com/purescript/purescript-random", 4 | "license": "BSD-3-Clause", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/purescript/purescript-random.git" 8 | }, 9 | "ignore": [ 10 | "**/.*", 11 | "bower_components", 12 | "node_modules", 13 | "output", 14 | "test", 15 | "bower.json", 16 | "package.json" 17 | ], 18 | "dependencies": { 19 | "purescript-effect": "^4.0.0", 20 | "purescript-integers": "^6.0.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "clean": "rimraf output && rimraf .pulp-cache", 5 | "build": "eslint src && pulp build -- --censor-lib --strict" 6 | }, 7 | "devDependencies": { 8 | "eslint": "^7.15.0", 9 | "pulp": "16.0.0-0", 10 | "purescript-psa": "^0.8.2", 11 | "rimraf": "^3.0.2" 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Effect/Random.js: -------------------------------------------------------------------------------- 1 | export const random = Math.random; 2 | -------------------------------------------------------------------------------- /src/Effect/Random.purs: -------------------------------------------------------------------------------- 1 | module Effect.Random where 2 | 3 | import Prelude 4 | 5 | import Effect (Effect) 6 | 7 | import Data.Int (toNumber, floor) 8 | 9 | -- | Returns a random number between 0 (inclusive) and 1 (exclusive). This is 10 | -- | a direct wrapper around JavaScript's `Math.random()`. 11 | foreign import random :: Effect Number 12 | 13 | -- | Takes a range specified by `low` (the first argument) and `high` (the 14 | -- | second), and returns a random integer uniformly distributed in the closed 15 | -- | interval `[low, high]`. It is unspecified what happens if `low > high`, 16 | -- | or if either of `low` or `high` is not an integer. 17 | -- | 18 | -- | For example: 19 | -- | ``` purescript 20 | -- | randomInt 1 10 >>= Console.print 21 | -- | ``` 22 | -- | will print a random integer between 1 and 10. 23 | randomInt :: Int -> Int -> Effect Int 24 | randomInt low high = do 25 | n <- random 26 | let asNumber = (toNumber high - toNumber low + one) * n + toNumber low 27 | pure $ floor asNumber 28 | 29 | -- | Returns a random number between a minimum value (inclusive) and a maximum 30 | -- | value (exclusive). It is unspecified what happens if `maximum < minimum`. 31 | -- | 32 | -- | For example: 33 | -- | ``` purescript 34 | -- | randomRange 1.0 2.0 >>= Console.print 35 | -- | ``` 36 | -- | will print a random number between 1 and 2. 37 | randomRange :: Number -> Number -> Effect Number 38 | randomRange min max = do 39 | n <- random 40 | pure (n * (max - min) + min) 41 | 42 | -- | Returns a random boolean value with an equal chance of being `true` or 43 | -- | `false`. 44 | randomBool :: Effect Boolean 45 | randomBool = (_ < 0.5) <$> random 46 | --------------------------------------------------------------------------------