diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index e8014e5..64aa48c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,66 +1,62 @@ -name: Publish - -on: - release: - types: [published] - push: - branches: - - master - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 12 - - run: npm ci - - run: node test.js - - publish-npm: - needs: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 12 - registry-url: https://registry.npmjs.org/ - scope: aspiesoft - - run: npm publish --access public - env: - NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} - - publish-gpr: - needs: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 12 - registry-url: https://npm.pkg.github.com/ - scope: aspiesoft - - run: npm publish - env: - AUTHOR_EMAIL: aspiesoftweb@gmail.com - AUTHOR_NAME: aspiesoft - NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} - - publish-npm-noscope: - needs: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 12 - registry-url: https://registry.npmjs.org/ - - name: remove scope from package name - run: | - sed -i 's/"name":\s"@aspiesoft\//"name":"/g' package.json - sed -i 's/"name":\s"@aspiesoft\//"name":"/g' package-lock.json - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} +name: Publish + +on: + workflow_dispatch + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + - run: npm ci + - run: node test.js + + publish-npm: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + scope: aspiesoft + - run: npm publish --access public + env: + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} + + publish-gpr: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: https://npm.pkg.github.com/ + scope: aspiesoft + - run: npm publish + env: + AUTHOR_EMAIL: aspiesoftweb@gmail.com + AUTHOR_NAME: aspiesoft + NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}} + + publish-npm-noscope: + needs: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + - name: remove scope from package name + run: | + sed -i 's/"name":\s"@aspiesoft\//"name":"/g' package.json + sed -i 's/"name":\s"@aspiesoft\//"name":"/g' package-lock.json + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} diff --git a/.npmignore b/.npmignore index 4ef5865..177cacd 100644 --- a/.npmignore +++ b/.npmignore @@ -1,6 +1,6 @@ -.npmignore -.github/ -test.js -test.html -build.js -script.js +.npmignore +.github/ +test.js +test.html +build.js +script.js diff --git a/LICENSE b/LICENSE index 0ae6e3c..7cbfcd8 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,21 @@ -The MIT License - -Copyright (c) 2020 aspiesoftweb@gmail.com - -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: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -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. +The MIT License + +Copyright (c) 2020 aspiesoftweb@gmail.com + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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. diff --git a/build.js b/build.js index 7b31239..43e5cd5 100644 --- a/build.js +++ b/build.js @@ -1,31 +1,31 @@ -const miniforge = require('@aspiesoft/miniforge-js'); - -miniforge.rootDir(__dirname); - -miniforge.build('./script.js', { - standAlone: true, - avoidDependencies: true, - compress: false, - encrypt: false, - outputNameMin: true, - minify: { - ecma: 6, - parse: {ecma: 6}, - compress: { - ecma: 6, - keep_infinity: false, - module: false, - passes: 5, - top_retain: ['random'], - typeofs: false, - }, - mangle: { - keep_classnames: true, - module: true, - reserved: ['random'], - }, - module: false, - keep_classnames: true, - ie8: true, - } -}); +const miniforge = require('@aspiesoft/miniforge-js'); + +miniforge.rootDir(__dirname); + +miniforge.build('./script.js', { + standAlone: true, + avoidDependencies: true, + compress: false, + encrypt: false, + outputNameMin: true, + minify: { + ecma: 6, + parse: {ecma: 6}, + compress: { + ecma: 6, + keep_infinity: false, + module: false, + passes: 5, + top_retain: ['random'], + typeofs: false, + }, + mangle: { + keep_classnames: true, + module: true, + reserved: ['random'], + }, + module: false, + keep_classnames: true, + ie8: true, + } +}); diff --git a/package-lock.json b/package-lock.json index 18b2b34..23cde4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,79 +1,79 @@ -{ - "name": "@aspiesoft/random-number-js", - "version": "1.1.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "@aspiesoft/miniforge-js": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@aspiesoft/miniforge-js/-/miniforge-js-1.0.0.tgz", - "integrity": "sha512-87zSX3jOkWLYEEaMsiH0e3XRgA4JYkSeHpxJh4CKGJskNms18rmMtRBb65K091r54vNFKSJksw2knOplALomwA==", - "dev": true, - "requires": { - "crypto-js": "^4.0.0", - "lzutf8": "^0.5.6", - "require-from-string": "^2.0.2", - "terser": "^4.8.0" - }, - "dependencies": { - "terser": { - "version": "4.8.0", - "resolved": "https://registry.npmjs.org/terser/-/terser-4.8.0.tgz", - "integrity": "sha512-EAPipTNeWsb/3wLPeup1tVPaXfIaU68xMnVdPafIL1TV05OhASArYyIfFvnvJCNrR2NIOvDVNNTFRa+Re2MWyw==", - "dev": true, - "requires": { - "commander": "^2.20.0", - "source-map": "~0.6.1", - "source-map-support": "~0.5.12" - } - } - } - }, - "buffer-from": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", - "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", - "dev": true - }, - "commander": { - "version": "2.20.3", - "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", - "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", - "dev": true - }, - "crypto-js": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.0.0.tgz", - "integrity": "sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg==", - "dev": true - }, - "lzutf8": { - "version": "0.5.6", - "resolved": "https://registry.npmjs.org/lzutf8/-/lzutf8-0.5.6.tgz", - "integrity": "sha512-nZT9HP+xe9FxhRj0DIHb/7jVeFdIQkOul8ebCYc5bMevmuzZZYbsjwFlg+UfO8GTQMfiGU2Fyt8JXtffeKF0Kg==", - "dev": true - }, - "require-from-string": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", - "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", - "dev": true - }, - "source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true - }, - "source-map-support": { - "version": "0.5.19", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz", - "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==", - "dev": true, - "requires": { - "buffer-from": "^1.0.0", - "source-map": "^0.6.0" - } - } - } -} +{ + "name": "@aspiesoft/random-number-js", + "version": "1.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@aspiesoft/miniforge-js": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@aspiesoft/miniforge-js/-/miniforge-js-1.0.0.tgz", + "integrity": "sha512-87zSX3jOkWLYEEaMsiH0e3XRgA4JYkSeHpxJh4CKGJskNms18rmMtRBb65K091r54vNFKSJksw2knOplALomwA==", + "dev": true, + "requires": { + "crypto-js": "^4.0.0", + "lzutf8": "^0.5.6", + "require-from-string": "^2.0.2", + "terser": "^4.8.0" + }, + "dependencies": { + "terser": { + "version": "4.8.0", + "resolved": "https://registry.npmjs.org/terser/-/terser-4.8.0.tgz", + "integrity": "sha512-EAPipTNeWsb/3wLPeup1tVPaXfIaU68xMnVdPafIL1TV05OhASArYyIfFvnvJCNrR2NIOvDVNNTFRa+Re2MWyw==", + "dev": true, + "requires": { + "commander": "^2.20.0", + "source-map": "~0.6.1", + "source-map-support": "~0.5.12" + } + } + } + }, + "buffer-from": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", + "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", + "dev": true + }, + "commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true + }, + "crypto-js": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-4.0.0.tgz", + "integrity": "sha512-bzHZN8Pn+gS7DQA6n+iUmBfl0hO5DJq++QP3U6uTucDtk/0iGpXd/Gg7CGR0p8tJhofJyaKoWBuJI4eAO00BBg==", + "dev": true + }, + "lzutf8": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/lzutf8/-/lzutf8-0.5.6.tgz", + "integrity": "sha512-nZT9HP+xe9FxhRj0DIHb/7jVeFdIQkOul8ebCYc5bMevmuzZZYbsjwFlg+UfO8GTQMfiGU2Fyt8JXtffeKF0Kg==", + "dev": true + }, + "require-from-string": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz", + "integrity": "sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw==", + "dev": true + }, + "source-map": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", + "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", + "dev": true + }, + "source-map-support": { + "version": "0.5.19", + "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.19.tgz", + "integrity": "sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==", + "dev": true, + "requires": { + "buffer-from": "^1.0.0", + "source-map": "^0.6.0" + } + } + } +} diff --git a/package.json b/package.json index ad1fd61..b08270f 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,26 @@ -{ - "name": "@aspiesoft/random-number-js", - "version": "1.2.0", - "description": "Generate random numbers with more variety.", - "license": "MIT", - "author": "AspieSoft", - "main": "script.min.js", - "scripts": { - "test": "node test.js", - "build": "node build.js" - }, - "repository": { - "type": "git", - "url": "git://github.com/aspiesoft/random-number-js.git" - }, - "keywords": [ - "random", - "number", - "js", - "javascript", - "nodejs" - ], - "devDependencies": { - "@aspiesoft/miniforge-js": "^1.0.0" - } -} +{ + "name": "@aspiesoft/random-number-js", + "version": "1.3.0", + "description": "Generate random numbers with more variety.", + "license": "MIT", + "author": "AspieSoft", + "main": "script.min.js", + "scripts": { + "test": "node test.js", + "build": "node build.js" + }, + "repository": { + "type": "git", + "url": "git://github.com/aspiesoft/random-number-js.git" + }, + "keywords": [ + "random", + "number", + "js", + "javascript", + "nodejs" + ], + "devDependencies": { + "@aspiesoft/miniforge-js": "^1.0.0" + } +} diff --git a/readme.md b/readme.md index 0a6b106..d65c95f 100644 --- a/readme.md +++ b/readme.md @@ -1,80 +1,90 @@ -# Random Number JS - -![npm version](https://img.shields.io/npm/v/@aspiesoft/random-number-js) -![GitHub top language](https://img.shields.io/github/languages/top/aspiesoft/random-number-js) -![GitHub license](https://img.shields.io/github/license/aspiesoft/random-number-js) - -![npm downloads](https://img.shields.io/npm/dw/@aspiesoft/random-number-js) -![npm downloads](https://img.shields.io/npm/dm/@aspiesoft/random-number-js) -![jsDelivr hits (GitHub)](https://img.shields.io/jsdelivr/gh/hm/aspiesoft/random-number-js) - -[![paypal](https://img.shields.io/badge/buy%20me%20a%20coffee-paypal-blue)](https://buymeacoffee.aspiesoft.com/) - -## Generate random numbers with more variety - -JavaScript's Math.random() function tends to generate the same number at times. -This random() function has a low chance of returning the same number twice, but still maintains that chance. - -JavaScript's Math.random() returns a decimal, which you would then multiply by 10, 100, 1000, ect and then floor. -This random() allows you to pass parameters for minimum and maximum number result, and still get a nice and random result. - -This function also runs fast, and uses the time in milliseconds to help add variety to its random results. -The function, also (optionally, true by default) keeps track of recent random results, and tries to avoid repeating them, but does so randomly. -The functions memory for recent random numbers is also cleared on a 1 second interval by default, and can be changed (or disabled) with a function as shown below. - -## Installation - -### node.js - -```shell script - -npm install @aspiesoft/random-number-js - -``` - -### cdn - -```html - - - -``` - -## Usage - -```JavaScript - -// require only if using node.js -const random = require('@aspiesoft/random-number-js'); - - -random(); // output: (25) random number between 0 and 100 - -random(10, 50); // output: (42) random number between 10 and 50 - -random(-50, 50, 2); // output: (23.02, -45.53, 2.5) random number between -50 and 50 - -random(0, 100, -1); // output: random number with a random decimal size between 0 and 10 - -random(0, 1, 10); // output: random number with a random decimal size between 0 and e^10 - -// optional enable lite mode (false by default) -// changes the default option, and can be (optionally) overwritten by the function -random.setLiteMode(true || false); // default: true (default for function attr, Not for the option) -// be default, the random function remembers past results for 1 second, and avoids repeating them. -// setting lite mode disables this feature, and reduces memory usage. -// the numbers will still often have variety, but with a higher chance of repeats - -// change the interval for clearing previous results -result.setClearInterval(1000); // default: 1000 (1000 milliseconds = 1 second) -result.setClearInterval(0); // disable interval - -random( - "", // default: 0 - "", // default: 100 - "", // default: 0 - // individually set lite mode for current function - true || false, // default: null (uses default option) -); - -``` +# Random Number JS + +![npm version](https://img.shields.io/npm/v/@aspiesoft/random-number-js) +![GitHub top language](https://img.shields.io/github/languages/top/aspiesoft/random-number-js) +![GitHub license](https://img.shields.io/github/license/aspiesoft/random-number-js) + +![npm downloads](https://img.shields.io/npm/dw/@aspiesoft/random-number-js) +![npm downloads](https://img.shields.io/npm/dm/@aspiesoft/random-number-js) +![jsDelivr hits (GitHub)](https://img.shields.io/jsdelivr/gh/hm/aspiesoft/random-number-js) + +[![paypal](https://img.shields.io/badge/buy%20me%20a%20coffee-paypal-blue)](https://buymeacoffee.aspiesoft.com/) + +## Generate random numbers with more variety + +JavaScript's Math.random() function tends to generate the same number at times. +This random() function has a low chance of returning the same number twice, but still maintains that chance. + +JavaScript's Math.random() returns a decimal, which you would then multiply by 10, 100, 1000, ect and then floor. +This random() allows you to pass parameters for minimum and maximum number result, and still get a nice and random result. + +This function also runs fast, and uses the time in milliseconds to help add variety to its random results. +The function, also (optionally, true by default) keeps track of recent random results, and tries to avoid repeating them, but does so randomly. +The functions memory for recent random numbers is also cleared on a 1 second interval by default, and can be changed (or disabled) with a function as shown below. + +## Whats New + +- added option to avoid repeat numbers within a radius of each other +- enabling lite mode globally now disables the clear interval automatically + +## Installation + +### node.js + +```shell script + +npm install @aspiesoft/random-number-js + +``` + +### cdn + +```html + + + +``` + +## Usage + +```JavaScript + +// require only if using node.js +const random = require('@aspiesoft/random-number-js'); + + +random(); // output: (25) random number between 0 and 100 + +random(10, 50); // output: (42) random number between 10 and 50 + +random(-50, 50, 2); // output: (23.02, -45.53, 2.5) random number between -50 and 50 + +random(0, 100, -1); // output: random number with a random decimal size between 0 and 10 + +random(0, 1, 10); // output: random number with a random decimal size between 0 and e^10 + +// reduce the chance of numbers being near each other +random.setAvoidRadius(10); // default: 0 +// will treat numbers within a distance of 10 of any previous number, as if it's a repeated number +// example: if output1 = 5 and output2 = 7, because 7-10 <= 5, output2 counts as a repeat of output1 + +// optional enable lite mode (false by default) +// changes the default option, and can be (optionally) overwritten by the function +random.setLiteMode(true || false); // default: true (default for function attr, Not for the option) +// be default, the random function remembers past results for 1 second, and avoids repeating them. +// setting lite mode disables this feature, and reduces memory usage. +// the numbers will still often have variety, but with a higher chance of repeats + +// change the interval for clearing previous results +result.setClearInterval(1000); // default: 1000 (1000 milliseconds = 1 second) +result.setClearInterval(0); // disable interval + +random( + "", // default: 0 + "", // default: 100 + "", // default: 0 + // individually set lite mode for current function (only works if lite mode is globally disabled) + true || false, // default: null (uses default option) +); + +``` diff --git a/script.js b/script.js index a08c065..e3ab9f4 100644 --- a/script.js +++ b/script.js @@ -1,224 +1,255 @@ -/*! https://github.com/AspieSoft/random-number-js v1.0.0 | (c) aspiesoftweb@gmail.com */ - -;const random = (function(){ - const options = { - liteMode: false, - }; - - let oldRand = []; - - function randomInt(min, max, liteMode = null){ - - // ensure - min = Number(min); - max = Number(max); - if(min === NaN || max === NaN){ - return NaN; - } - - // ensure min < max - if(min > max){ - let temp = min; - min = max; - max = temp; - } - - // return simple number if min is close or equal to max - if(min === max){ - return min; - }else if(min + 1 === max){ - if(Math.floor(Math.random()*10) % 2 === 0){ - return min; - }else{ - return max; - } - } - - // use time millis for random numbers - let time = (new Date()).getTime().toString().slice(-10); - let maxLen = Math.max(Math.abs(max).toString().length, Math.abs(min).toString().length); - let randMaxLength = Math.pow(10, maxLen); - let result = ''; - - // select random index from time millis - while(result.length < maxLen){ - result += time[Math.floor(Math.random()*10)]; - // random chance to cut number short - if(Math.floor(Math.random()*randMaxLength) % 2 === 0 && Math.floor(Math.random()*randMaxLength) % 2 === 0){ - break; - } - } - - let randLength = Math.pow(10, result.length); - - result = result.split(''); - - // go through random number indexes with random chance to change index to a new random number - for(let i = 0; i < result.length; i++){ - let rand1 = Math.floor(Math.random()*10); - let rand2 = Math.floor(Math.random()*randLength); - let rand3 = Math.floor(Math.random()*randLength); - if((rand1 % 2 === 0 && rand2 % 2 === 0 && rand3 % 2 === 0) || (rand1 % 2 !== 0 && ((rand2 % 2 === 0 && rand3 % 2 !== 0) || (rand2 % 2 !== 0 && rand3 % 2 === 0)))){ - result[i] = Math.floor(Math.random()*10).toString(); - } - } - - if(Math.floor(Math.random()*100) % 2 === 0){ - result.reverse(); - } - - if(result[0] === '0'){ - result[0] = '1'; - } - - // convert number string, to actual number - result = Number(result.join('')); - - // if min or max is negative, add random chance to switch to negative (absolute if both are negative) - if(min < 0 && max < 0){ - result *= -1; - }else if((min < 0 || max < 0) && Math.floor(Math.random()*100) % 2 === 0){ - result *= -1; - } - - // ensure number is not over the max value - if(max > 0){ - while(result > max){ - result -= max-1; - } - }else if(max < 0){ - while(result > max){ - result += max-1; - } - }else if(max === 0 && result > 0){ - return randomInt(min, max, liteMode); - } - - // ensure number is not under the min value - if(min > 0){ - while(result < min){ - result += min+1; - } - }else if(min < 0){ - while(result < min){ - result -= min+1; - } - }else if(min === 0 && result < 0){ - return randomInt(min, max, liteMode); - } - - // ensure min did not increase the number too much (reduce max by reduced min value instead of by max value) - if(max > 0){ - while(result > max){ - result -= min-1; - } - }else if(max < 0){ - while(result > max){ - result += min-1; - } - }else if(max === 0 && result > 0){ - return randomInt(min, max, liteMode); - } - - // ensure result is not too small or too big or retry (this very rarely could happen) - if(result < min || result > max){ - return randomInt(min, max, liteMode); - } - - // skip tracking previous results, if in lite mode - if(liteMode || (liteMode !== false && options.liteMode)){ - return result; - } - - // keep track of old results, and reduce chance of repeats - let preRand = [...oldRand]; - while(preRand.includes(result)){ - if(Math.floor(Math.random()*100) % 2 === 0){ - preRand.splice(preRand.indexOf(result), 1); - } - if(Math.floor(Math.random()*100) % 2 === 0 && Math.floor(Math.random()*100) % 2 === 0){ - oldRand.splice(oldRand.indexOf(result), 1); - } - if(Math.floor(Math.random()*100) % 2 === 0 || Math.floor(Math.random()*100) % 2 === 0){ - return randomInt(min, max, liteMode); - } - } - oldRand.push(result); - - return result; - } - - - function randomDouble(min = 0, max = 100, decimalLength = 0, lite = null){ - - // ensure min and max are numbers - min = Number(min); - max = Number(max); - decimalLength = Number(decimalLength); - if(min === NaN || max === NaN || decimalLength === NaN){ - return 'NaN'; - } - - // ensure min < max - if(min > max){ - let temp = min; - min = max; - max = temp; - } - - let decimalLenInt = Math.floor(decimalLength); - if(decimalLenInt !== decimalLength){ - decimalLenInt++; - } - if(decimalLenInt < 0){ - decimalLenInt *= -1; - } - - if(decimalLength < 0){ - decimalLength = Math.floor(Math.random()*10); - } - - let result = randomInt(min, max, lite); - if(result < min || result >= max || decimalLength == 0){ - return result; - } - - let decimalLen10 = Math.pow(10, decimalLenInt); - - if(Math.floor(Math.random()*100) % 2 === 0 || Math.floor(Math.random()*100) % 2 === 0){ - let decimal = randomInt(0, decimalLen10-1, lite); - decimal = decimal.toString(); - while(decimal.length < decimalLenInt && Math.floor(Math.random()*100) % 2 === 0){ - decimal = '0'+decimal; - } - decimal = Number('0.'+decimal); - result += decimal; - } - - return Math.round(result*decimalLen10)/decimalLen10; - } - - let interval = setInterval(function(){ - oldRand = []; - }, 1000); - - const result = randomDouble; - result.setLiteMode = function(set = true){ - options.liteMode = !!set; - }; - result.setClearInterval = function(ms = 1000){ - clearInterval(interval); - if(ms && typeof ms === 'number' && ms > 0){ - interval = setInterval(function(){ - oldRand = []; - }, ms); - } - }; - - return result; -})(); - -if(typeof module === 'object'){ - // add node.js compatibility - module.exports = random; -} +/*! https://github.com/AspieSoft/random-number-js v1.0.0 | (c) aspiesoftweb@gmail.com */ + +;const random = (function(){ + const options = { + liteMode: false, + avoidRadius: 0, + }; + + let oldRand = []; + + function randomInt(min, max, liteMode = null, isDecimal = false){ + + // ensure + min = Number(min); + max = Number(max); + if(min === NaN || max === NaN){ + return NaN; + } + + // ensure min < max + if(min > max){ + let temp = min; + min = max; + max = temp; + } + + // return simple number if min is close or equal to max + if(min === max){ + return min; + }else if(min + 1 === max){ + if(Math.floor(Math.random()*10) % 2 === 0){ + return min; + }else{ + return max; + } + } + + // use time millis for random numbers + let time = (new Date()).getTime().toString().slice(-10); + let maxLen = Math.max(Math.abs(max).toString().length, Math.abs(min).toString().length); + let randMaxLength = Math.pow(10, maxLen); + let result = ''; + + // select random index from time millis + while(result.length < maxLen){ + result += time[Math.floor(Math.random()*10)]; + // random chance to cut number short + if(Math.floor(Math.random()*randMaxLength) % 2 === 0 && Math.floor(Math.random()*randMaxLength) % 2 === 0){ + break; + } + } + + let randLength = Math.pow(10, result.length); + + result = result.split(''); + + // go through random number indexes with random chance to change index to a new random number + for(let i = 0; i < result.length; i++){ + let rand1 = Math.floor(Math.random()*10); + let rand2 = Math.floor(Math.random()*randLength); + let rand3 = Math.floor(Math.random()*randLength); + if((rand1 % 2 === 0 && rand2 % 2 === 0 && rand3 % 2 === 0) || (rand1 % 2 !== 0 && ((rand2 % 2 === 0 && rand3 % 2 !== 0) || (rand2 % 2 !== 0 && rand3 % 2 === 0)))){ + result[i] = Math.floor(Math.random()*10).toString(); + } + } + + if(Math.floor(Math.random()*100) % 2 === 0){ + result.reverse(); + } + + if(isDecimal && result[0] === '0'){ + result[0] = '1'; + } + + // convert number string, to actual number + if(result.length > 0){ + result = Number(result.join('')); + }else{ + result = 0; + } + + // if min or max is negative, add random chance to switch to negative (absolute if both are negative) + if(min < 0 && max < 0){ + result *= -1; + }else if((min < 0 || max < 0) && Math.floor(Math.random()*100) % 2 === 0){ + result *= -1; + } + + // ensure number is not over the max value + if(max > 0){ + while(result > max){ + result -= max-1; + } + }else if(max < 0){ + while(result > max){ + result += max-1; + } + }else if(max === 0 && result > 0){ + return randomInt(min, max, liteMode); + } + + // ensure number is not under the min value + if(min > 0){ + while(result < min){ + result += min+1; + } + }else if(min < 0){ + while(result < min){ + result -= min+1; + } + }else if(min === 0 && result < 0){ + return randomInt(min, max, liteMode); + } + + // ensure min did not increase the number too much (reduce max by reduced min value instead of by max value) + if(max > 0){ + while(result > max){ + result -= min-1; + } + }else if(max < 0){ + while(result > max){ + result += min-1; + } + }else if(max === 0 && result > 0){ + return randomInt(min, max, liteMode); + } + + // ensure result is not too small or too big or retry (this very rarely could happen) + if(result < min || result > max){ + return randomInt(min, max, liteMode); + } + + // skip tracking previous results, if in lite mode + if(liteMode || (liteMode !== false && options.liteMode)){ + return result; + } + + // keep track of old results, and reduce chance of repeats + let preRand = [...oldRand]; + + if(options.avoidRadius !== 0){ + for(let i = 0; i < preRand.length; i++){ + if(preRand[i] >= result-options.avoidRadius && preRand[i] <= result+options.avoidRadius){ + if(Math.floor(Math.random()*100) % 2 === 0){ + return randomInt(min, max, liteMode); + } + } + } + } + + while(preRand.includes(result)){ + if(Math.floor(Math.random()*100) % 2 === 0){ + preRand.splice(preRand.indexOf(result), 1); + } + if(Math.floor(Math.random()*100) % 2 === 0 && Math.floor(Math.random()*100) % 2 === 0){ + oldRand.splice(oldRand.indexOf(result), 1); + } + if(Math.floor(Math.random()*100) % 2 === 0 || Math.floor(Math.random()*100) % 2 === 0){ + return randomInt(min, max, liteMode); + } + } + oldRand.push(result); + + return result; + } + + + function randomDouble(min = 0, max = 100, decimalLength = 0, lite = null){ + + // ensure min and max are numbers + min = Number(min); + max = Number(max); + decimalLength = Number(decimalLength); + if(min === NaN || max === NaN || decimalLength === NaN){ + return 'NaN'; + } + + // ensure min < max + if(min > max){ + let temp = min; + min = max; + max = temp; + } + + let decimalLenInt = Math.floor(decimalLength); + if(decimalLenInt !== decimalLength){ + decimalLenInt++; + } + if(decimalLenInt < 0){ + decimalLenInt *= -1; + } + + if(decimalLength < 0){ + decimalLength = Math.floor(Math.random()*10); + } + + let result = randomInt(min, max, lite, false); + if(result < min || result >= max || decimalLength == 0){ + return result; + } + + let decimalLen10 = Math.pow(10, decimalLenInt); + + if(Math.floor(Math.random()*100) % 2 === 0 || Math.floor(Math.random()*100) % 2 === 0){ + let decimal = randomInt(0, decimalLen10-1, lite, true); + decimal = decimal.toString(); + while(decimal.length < decimalLenInt && Math.floor(Math.random()*100) % 2 === 0){ + decimal = '0'+decimal; + } + decimal = Number('0.'+decimal); + result += decimal; + } + + return Math.round(result*decimalLen10)/decimalLen10; + } + + let hasInterval = true; + let interval = setInterval(function(){ + oldRand = []; + }, 1000); + + const result = randomDouble; + + result.setLiteMode = function(set = true){ + options.liteMode = !!set; + if(options.liteMode){ + result.setClearInterval(0); + }else if(!hasInterval){ + result.setClearInterval(); + } + }; + + result.setAvoidRadius = function(set = 0){ + options.avoidRadius = Number(set) || 0; + }; + + result.setClearInterval = function(ms = 1000){ + clearInterval(interval); + if(ms && typeof ms === 'number' && ms > 0){ + hasInterval = true; + interval = setInterval(function(){ + oldRand = []; + }, ms); + }else{ + hasInterval = false; + } + }; + + return result; +})(); + +if(typeof module === 'object'){ + // add node.js compatibility + module.exports = random; +} diff --git a/script.min.js b/script.min.js index f712878..c910943 100644 --- a/script.min.js +++ b/script.min.js @@ -1,2 +1,2 @@ -/*! https://github.com/AspieSoft/random-number-js v1.0.0 | (c) aspiesoftweb@gmail.com */ -const random=function(){const t={liteMode:!1};let r=[];function o(e,a,n=null){if(e=Number(e),a=Number(a),e===NaN||a===NaN)return NaN;if(e>a){let t=e;e=a,a=t}if(e===a)return e;if(e+1===a)return Math.floor(10*Math.random())%2==0?e:a;let l=(new Date).getTime().toString().slice(-10),f=Math.max(Math.abs(a).toString().length,Math.abs(e).toString().length),h=Math.pow(10,f),M="";for(;M.length0)for(;M>a;)M-=a-1;else if(a<0)for(;M>a;)M+=a-1;else if(0===a&&M>0)return o(e,a,n);if(e>0)for(;M0)for(;M>a;)M-=e-1;else if(a<0)for(;M>a;)M+=e-1;else if(0===a&&M>0)return o(e,a,n);if(Ma)return o(e,a,n);if(n||!1!==n&&t.liteMode)return M;let u=[...r];for(;u.includes(M);)if(Math.floor(100*Math.random())%2==0&&u.splice(u.indexOf(M),1),Math.floor(100*Math.random())%2==0&&Math.floor(100*Math.random())%2==0&&r.splice(r.indexOf(M),1),Math.floor(100*Math.random())%2==0||Math.floor(100*Math.random())%2==0)return o(e,a,n);return r.push(M),M}let e=setInterval((function(){r=[]}),1e3);const a=function(t=0,r=100,e=0,a=null){if(t=Number(t),r=Number(r),e=Number(e),t===NaN||r===NaN||e===NaN)return"NaN";if(t>r){let o=t;t=r,r=o}let n=Math.floor(e);n!==e&&n++,n<0&&(n*=-1),e<0&&(e=Math.floor(10*Math.random()));let l=o(t,r,a);if(l=r||0==e)return l;let f=Math.pow(10,n);if(Math.floor(100*Math.random())%2==0||Math.floor(100*Math.random())%2==0){let t=o(0,f-1,a);for(t=t.toString();t.length0&&(e=setInterval((function(){r=[]}),t))},a}();"object"==typeof module&&(module.exports=random); +/*! https://github.com/AspieSoft/random-number-js v1.0.0 | (c) aspiesoftweb@gmail.com */ +const random=function(){const t={liteMode:!1,avoidRadius:0};let o=[];function r(e,a,n=null,l=!1){if(e=Number(e),a=Number(a),e===NaN||a===NaN)return NaN;if(e>a){let t=e;e=a,a=t}if(e===a)return e;if(e+1===a)return Math.floor(10*Math.random())%2==0?e:a;let f=(new Date).getTime().toString().slice(-10),i=Math.max(Math.abs(a).toString().length,Math.abs(e).toString().length),h=Math.pow(10,i),M="";for(;M.length0?Number(M.join("")):0,(e<0&&a<0||(e<0||a<0)&&Math.floor(100*Math.random())%2==0)&&(M*=-1),a>0)for(;M>a;)M-=a-1;else if(a<0)for(;M>a;)M+=a-1;else if(0===a&&M>0)return r(e,a,n);if(e>0)for(;M0)for(;M>a;)M-=e-1;else if(a<0)for(;M>a;)M+=e-1;else if(0===a&&M>0)return r(e,a,n);if(Ma)return r(e,a,n);if(n||!1!==n&&t.liteMode)return M;let d=[...o];if(0!==t.avoidRadius)for(let o=0;o=M-t.avoidRadius&&d[o]<=M+t.avoidRadius&&Math.floor(100*Math.random())%2==0)return r(e,a,n);for(;d.includes(M);)if(Math.floor(100*Math.random())%2==0&&d.splice(d.indexOf(M),1),Math.floor(100*Math.random())%2==0&&Math.floor(100*Math.random())%2==0&&o.splice(o.indexOf(M),1),Math.floor(100*Math.random())%2==0||Math.floor(100*Math.random())%2==0)return r(e,a,n);return o.push(M),M}let e=!0,a=setInterval((function(){o=[]}),1e3);const n=function(t=0,o=100,e=0,a=null){if(t=Number(t),o=Number(o),e=Number(e),t===NaN||o===NaN||e===NaN)return"NaN";if(t>o){let r=t;t=o,o=r}let n=Math.floor(e);n!==e&&n++,n<0&&(n*=-1),e<0&&(e=Math.floor(10*Math.random()));let l=r(t,o,a,!1);if(l=o||0==e)return l;let f=Math.pow(10,n);if(Math.floor(100*Math.random())%2==0||Math.floor(100*Math.random())%2==0){let t=r(0,f-1,a,!0);for(t=t.toString();t.length0?(e=!0,a=setInterval((function(){o=[]}),t)):e=!1},n}();"object"==typeof module&&(module.exports=random); diff --git a/test.html b/test.html index 1a06416..168012a 100644 --- a/test.html +++ b/test.html @@ -1,11 +1,12 @@ - - + + diff --git a/test.js b/test.js index c48ea9f..6b5ba66 100644 --- a/test.js +++ b/test.js @@ -1,8 +1,9 @@ -const random = require('./script.min'); - -random.setLiteMode(); -random.setClearInterval(0); - -for(let i = 0; i < 10; i++){ - console.log(random()); -} +const random = require('./script.min'); + +random.setLiteMode(); +random.setClearInterval(0); +random.setAvoidRadius(10); + +for(let i = 0; i < 10; i++){ + console.log(random()); +}