Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

random-array-ensure

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

random-array-ensure

Retrieve random elements from an array, ensuring that al least each item will be received one time and that no duplicates will be received after each array loop.

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

random-array-ensure

Don't you hate that when you get a random element from an array, sometimes the randomizer gets the same entry in a short period?

This library will ensure that for a given array, you will receive a random element one time per loop (A.K.A. shuffle) with the addition that when all items are received, will shuffle the array again ensuring that the last elements of the old shuffle will not be on the first positions the next time. This way you will have a completely random array but with no "duplicated" entries feeling.

So, for a length = 1000 array and ensure = 0.25 (25%), you will get 1000 random entries without any duplicate and then will shuffle the array again to get another 1000 random entries without any duplicate, but ensuring that 25% of the last entries received in the old shuffle won't be stored on the first 25% positions of the next shuffle, increasing the "randomness without duplication" feeling.

Install

npm install random-array-ensure

Usage

import { RandomEnsure } from random-array-ensure;

let list = new RandomEnsure([0, 1, 2, 3, 4]);

for (let i = 0; i < 1000; i++) {
    let elem = list.next(); // Get next random ensured element.
}

// Giving a custom random() function
let list = new RandomEnsure([0, 1, 2, 3, 4], {
    random: () => {
        // Your custom function that returns a number between 0 and 1
        // must be 0 inclusive and 1 exclusive
        // Default is Math.random()
        return Math.random();
    }
});

// Giving a custom ensuring option
let list = new RandomEnsure([0, 1, 2, 3, 4], {
    // A number between 0 and 0.5
    // or an absolute number between 1 and half of the array length
    // Default is 0.25
    ensure: 0.5
});

Keywords

FAQs

Package last updated on 23 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc