├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v2 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /* eslint-disable import/export */ 2 | 3 | /** 4 | Generate a random [float](https://en.wikipedia.org/wiki/Floating_point). 5 | 6 | @param minimum - Minimum float to return. Default: `1`. 7 | @param maximum - Maximum float to return. Default: `1`. 8 | @returns A float from `minimum` to `maximum`. 9 | 10 | @example 11 | ``` 12 | import randomFloat from 'random-float'; 13 | 14 | randomFloat(5); 15 | //=> 4.401887938147411 16 | 17 | randomFloat(10, 100); 18 | //=> 72.34217455144972 19 | ``` 20 | */ 21 | export default function randomFloat(maximum?: number): number; 22 | export default function randomFloat(minimum: number, maximum: number): number; // eslint-disable-line @typescript-eslint/unified-signatures 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function randomFloat(minimum, maximum) { 2 | if (maximum === undefined) { 3 | maximum = minimum; 4 | minimum = 0; 5 | } 6 | 7 | if (typeof minimum !== 'number' || typeof maximum !== 'number') { 8 | throw new TypeError('Expected all arguments to be numbers'); 9 | } 10 | 11 | return (Math.random() * (maximum - minimum)) + minimum; 12 | } 13 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import randomFloat from './index.js'; 3 | 4 | expectType(randomFloat()); 5 | expectType(randomFloat(5)); 6 | expectType(randomFloat(10, 100)); 7 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "random-float", 3 | "version": "3.0.0", 4 | "description": "Generate a random float", 5 | "license": "MIT", 6 | "repository": "sindresorhus/random-float", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "random", 27 | "randomize", 28 | "float", 29 | "floating", 30 | "point", 31 | "real", 32 | "number", 33 | "max", 34 | "min", 35 | "math", 36 | "generate", 37 | "generator" 38 | ], 39 | "devDependencies": { 40 | "ava": "^3.15.0", 41 | "in-range": "^3.0.0", 42 | "number-is-float": "^2.0.0", 43 | "stable-fn": "^3.0.0", 44 | "tsd": "^0.14.0", 45 | "xo": "^0.38.2" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # random-float 2 | 3 | > Generate a random [float](https://en.wikipedia.org/wiki/Floating_point) 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install random-float 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import randomFloat from 'random-float'; 15 | 16 | randomFloat(5); 17 | //=> 4.401887938147411 18 | 19 | randomFloat(10, 100); 20 | //=> 72.34217455144972 21 | ``` 22 | 23 | ## API 24 | 25 | ### randomFloat(maximum?) 26 | 27 | Returns an float from `0` to `maximum`. 28 | 29 | ### randomFloat(minimum, maximum) 30 | 31 | Returns an float from `minimum` to `maximum`. 32 | 33 | #### minimum 34 | 35 | Type: `number`\ 36 | Default: `0` 37 | 38 | Minimum float to return. 39 | 40 | #### maximum 41 | 42 | Type: `number`\ 43 | Default: `1` 44 | 45 | Maximum float to return. 46 | 47 | ## Related 48 | 49 | - [random-int](https://github.com/sindresorhus/random-int) - Generate a random integer 50 | - [random-item](https://github.com/sindresorhus/random-item) - Get a random item from an array 51 | - [random-boolean](https://github.com/arthurvr/random-boolean) - Get a random boolean 52 | - [random-obj-key](https://github.com/sindresorhus/random-obj-key) - Get a random key from an object 53 | - [random-obj-prop](https://github.com/sindresorhus/random-obj-prop) - Get a random property from an object 54 | - [unique-random](https://github.com/sindresorhus/unique-random) - Generate random numbers that are consecutively unique 55 | - [unique-random-array](https://github.com/sindresorhus/unique-random-array) - Get consecutively unique elements from an array 56 | - [crypto-random-string](https://github.com/sindresorhus/crypto-random-string) - Generate a cryptographically strong random string 57 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import numberIsFloat from 'number-is-float'; 3 | import inRange from 'in-range'; 4 | import stableFn from 'stable-fn'; 5 | import randomFloat from './index.js'; 6 | 7 | const inRangeCheck = ({start, end}) => stableFn(() => inRange(randomFloat(start, end), {start, end})); 8 | 9 | test('main', t => { 10 | t.true(numberIsFloat(randomFloat(0, 10))); 11 | t.false(stableFn(() => randomFloat(1))); 12 | t.true(stableFn(() => inRange(randomFloat(1), {start: 0, end: 1}))); 13 | t.true(inRange(randomFloat(1), {start: 0, end: 1})); 14 | t.true(inRangeCheck({start: 0, end: 0.1})); 15 | t.true(inRangeCheck({start: 0, end: 1})); 16 | t.true(inRangeCheck({start: 0, end: 2})); 17 | t.true(inRangeCheck({start: 0, end: 10})); 18 | t.true(inRangeCheck({start: 0, end: 100})); 19 | t.true(inRangeCheck({start: 0, end: 0})); 20 | t.true(inRangeCheck({start: 0, end: 1})); 21 | t.true(inRangeCheck({start: 0, end: 2})); 22 | t.true(inRangeCheck({start: 0, end: 10})); 23 | t.true(inRangeCheck({start: 0, end: 100})); 24 | t.true(inRangeCheck({start: 5, end: 10})); 25 | t.true(inRangeCheck({start: 524234213, end: 99999999999})); 26 | t.true(inRangeCheck({start: -10, end: -5})); 27 | }); 28 | --------------------------------------------------------------------------------