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

id-16

v1.0.5

Published

Create random id's and numbers in base 16!

Downloads

98

Readme

Build Status npm Standard - JavaScript Style Guide

Intro

Use the simple function to quickly generate a random base 16 (hexadecimal) number or create a generator and then use it to create loads of random numbers of the same length which are checked for collisions. This is useful for creating unique IDs and whatnot.

Basic usage

Create a random hex number:

var id_16 = require('id-16')
var random = id_16(10)
// e.g. 63f499b6f4

Create a generator and use it:

var id_16 = require('id-16')
var id = id_16.generator(4)
user_1 = id() // -> 63f4
user_2 = id() // -> b6ab

Documentation

All the current functions are documented. If you have any comments or suggestions please go here.

id_16([length])

This function creates a random number in base 16. Returns: A number in base 16.

| param | type | description | | --- | --- | ---| | lenght | Number optional | The desired length of the return number. (Default: 8) |

id_16.generator([length, expandby])

This function returns a function that creates random base 16 numbers. This generator checks the numbers for collisions. If too many collisions occur an error is thrown or (if expandby is defined) the length of the number is expanded. returns: The generator

| param | type | description | | --- | --- | ---| | lenght | Number optional | The desired length of the return number. (Default: 8) | | expandby | Number optional | Expand the length of the return number by this number if too many collisions occur.|

This generator also has some properties and methods:

generator.remove(number)

Removes a number from the list of numbers that have been created by this generator, allowing the number to be created again.

| param | type | description | | --- | --- | ---| | number | Number | The number that you want to delete from the list of already 'used' numbers by this generator. |

generator.list

An array containing all the numbers that have been created by this generator. It's used to check for collisions by the generator itself but it is open to interact with.

creation of random numbers

The program currently uses the following function to generate the numbers:

var random_number = Math.floor(Math.random() * 16)

This creates a number between 0 and 15 after which the number is then converted into base 16. This process is then repeated for the desired length of the number and the numbers are added onto a string. This string is what's returned by the function.

Is it truly random?

Many people state that Math.random() is not truly random. I'm not going to debate whether or not this is true however I am going to show you the results I got. I ran the function in a loop creating 1000 random numbers between 0 and 15, the following graph shows just how many times each number was returned by the function:

As you can see the numbers are all created about as many times. I think it's good enough for this purpose but I'm open to any suggestions! So please let me know if you think you've got a better way of creating truly random numbers.

Suggestions

If you have any suggestions or want something to be implemented, please submit an issue or open a pull request!

License & Disclaimer

This module is open-sourced under the MIT Licence (see LICENSE for the full license). So within some limits, you can do with the code whatever you want.

The software is provided as-is. It might work as expected — or not. Just don't blame me.