unique-random
Generate random numbers that are consecutively unique
Last updated 6 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install unique-random 
SYNC missed versions from official npm registry.

unique-random

Generate random numbers that are consecutively unique

Useful for things like slideshows where you don't want to have the same slide twice in a row.

Install

npm install unique-random

Usage

import {consecutiveUniqueRandom} from 'unique-random';

const random = consecutiveUniqueRandom(1, 10);

console.log(random(), random(), random());
//=> 5 2 6

API

consecutiveUniqueRandom(minimum, maximum)

Generate random numbers that are consecutively unique, meaning that each number in the sequence is distinct from the one immediately before it.

exhaustiveUniqueRandom(minimum, maximum)

Generate random numbers that do not repeat until the entire range has appeared.

consecutiveUniqueRandom and exhaustiveUniqueRandom

Returns a function, that when called, will return the generated number.

The returned function is also an iterable which consumes from the same source as the function:

import {exhaustiveUniqueRandom} from 'unique-random';

const random = exhaustiveUniqueRandom(1, 10);

for (const number of random) {
	console.log(number);

	// The unique numbers will be iterated over infinitely
	if (stopCondition) {
		break;
	}
}

[!NOTE] If minimum is equal to maximum, the same value will always be returned.

Related

Current Tags

  • 4.0.0                                ...           latest (7 months ago)

7 Versions

  • 4.0.0                                ...           7 months ago
  • 3.0.0                                ...           4 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.0                                ...           7 years ago
  • 1.0.0                                ...           10 years ago
  • 0.1.1                                ...           11 years ago
  • 0.1.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (3)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE