Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kdby-io/range-shuffle
Wide-range integer shuffler
https://github.com/kdby-io/range-shuffle
lcg random range shuffle
Last synced: about 1 month ago
JSON representation
Wide-range integer shuffler
- Host: GitHub
- URL: https://github.com/kdby-io/range-shuffle
- Owner: kdby-io
- License: mit
- Created: 2017-05-17T22:50:18.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-29T05:30:22.000Z (over 4 years ago)
- Last Synced: 2024-10-05T08:55:13.472Z (about 1 month ago)
- Topics: lcg, random, range, shuffle
- Language: JavaScript
- Homepage:
- Size: 1 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Range-Shuffle [![Build Status](https://img.shields.io/travis/pueue/range-shuffle.svg?style=flat-square)](https://travis-ci.org/pueue/range-shuffle) [![Version](https://img.shields.io/npm/v/range-shuffle.svg?style=flat-square)](https://www.npmjs.com/package/range-shuffle) [![Download](https://img.shields.io/npm/dt/range-shuffle.svg?style=flat-square)](https://www.npmjs.com/package/range-shuffle) [![Greenkeeper badge](https://badges.greenkeeper.io/pueue/range-shuffle.svg)](https://greenkeeper.io/)
Wide-range integer shuffler.
## Goals
- No array
- Reversible## Getting Started
### Install
```sh
npm install --save range-shuffle
```### Usage
```javascript
const Shuffler = require('range-shuffle');
// or
import Shuffler from 'range-shuffle';const rs = new Shuffler({
MULTIPLIER: 7, // it must be a prime number
INCREMENT: 9,
MODULUS: 10,
});const array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const shuffled = array.map(int => rs.LCG(int));
// [9, 6, 3, 0, 7, 4, 1, 8, 5, 2]
const reversed = shuffled.map(int => rs.reverseLCG(int));
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
```## Principle
[Linear congruential generator](https://en.wikipedia.org/wiki/Linear_congruential_generator)
## Note
All constants & method arguments can be both `Number` and `String` type.
If you want to use a large number over `Number.MAX_SAFE_INTEGER`(9007199254740991), use a `String` type input like `.LCG('9999999999999999')`.## API
### `new Shuffler(constants)`
`constants` is a object that must contain **three constants**:
- `MULTIPLIER`: Must be **a prime number**.
- `INCREMENT`
- `MODULUS`: Range for shuffling. `0 ~ (MODULUS - 1)`### `.LCG(integer)`
Performs linear congruential generating based on constants.
### `.reverseLCG(integer)`
Restores linear congruential generating based on constants.