npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

random-number-js

v1.3.3

Published

Generate random numbers with more variety.

Downloads

9

Readme

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/[email protected]/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)
);