random-number-js

1.3.3 • Public • Published

Random Number JS

npm version GitHub top language GitHub license

npm downloads npm downloads jsDelivr hits (GitHub)

paypal

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

npm install @aspiesoft/random-number-js

cdn

<script src="https://cdn.jsdelivr.net/gh/AspieSoft/random-number-js@1.3.2/script.min.js"></script>

Usage

// 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
random.setClearInterval(1000); // default: 1000 (1000 milliseconds = 1 second)
random.setClearInterval(0); // disable interval

random(
  "<insert minimum value>", // default: 0
  "<insert maximum value>", // default: 100
  "<insert decimal size>", // default: 0
  // individually set lite mode for current function (only works if lite mode is globally disabled)
  true || false, // default: null (uses default option)
);

Package Sidebar

Install

npm i random-number-js

Weekly Downloads

11

Version

1.3.3

License

MIT

Unpacked Size

8.25 kB

Total Files

4

Last publish

Collaborators

  • aspiesoft