Code Monkey home page Code Monkey logo

unique-random's Introduction

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

unique-random's People

Contributors

aweary avatar bendingbender avatar richienb avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unique-random's Issues

Why are my test results repeated?

app.mjs:

import uniqueRandom from 'unique-random';

const random = uniqueRandom(1, 5);

console.log(random(), random(), random());

results:

PS D:\app\node-demo> node app.mjs
3 5 2
PS D:\app\node-demo> node app.mjs
4 1 5
PS D:\app\node-demo> node app.mjs
5 3 2
PS D:\app\node-demo> node app.mjs
2 1 2
PS D:\app\node-demo> node app.mjs
3 2 1
PS D:\app\node-demo> node app.mjs
4 1 2
PS D:\app\node-demo> node app.mjs
4 5 4

Support optional depth argument

Currently unique-random only guarantees a unique random value at a depth of 1 in the call history. We can potentially add a third optional argument depth where users can specifiy how many calls deep the returned value should be compared against. So something like

var rand = uniqueRandom(1, 10, 3);

would mean that the returned value would be unique relative to the last 3 calls.

I'm just wondering if this is something you'd be interested in supporting. If so I can PR. I can fork it otherwise.

same value?

const uniqueRandom = require("unique-random")

const random = uniqueRandom(0, 2);

console.log(random(), random(), random());

Not Working

Just installed the package, cool idea BTW!
Unfortunately I think it's not working correctly.
the following code:

import uniqueRandom from "unique-random";

const random = uniqueRandom(1, 4);
console.log(random(), random(), random(), random());

sometimes has bad results (i.e 1, 2, 4, 2 or 4, 1, 3, 4). I've noticed it's never the same number twice in a row if that's helpfull.

Why write this in such a "clever" way?

I was reviewing this function's code, and was surprised by how dense it was written. Just curious if you still support this style of code. (2 and a half years is 20-20?)

Admittedly it is perfectly effective, just far from readable. Like this line:

return prev = num === prev && min !== max ? rand() : num;

๐Ÿ˜„

Test when min == max

(theoretically) If they are equal then the previous value will always be the same, so the program enters an infinite loop.

I think it is a better fix to make it work with only one number anyways, as opposed to throwing an error since this could happen if min and max are generated by another source.

They repeat... LOL

Heya. I haven't been much active for a long time.

Just tired that package and.. man, several runs and always get a duplicates.

const random = uniqueRandom(1, 100);

console.log(
    Array(30)
        .fill(0)
        .map(() => random())
        .sort((a, b) => a - b)
);

outputs

[
   1,  7,   7, 10, 11, 12, 13, 24, 27,
  38, 39,  41, 43, 47, 51, 52, 59, 62,
  66, 67,  68, 70, 72, 82, 82, 84, 85,
  94, 99, 100
]

this one is not sorted, but at least the 83 is repeated.

[
  41, 16, 61,  2, 30, 56, 71, 20, 24,
  53, 73, 47, 23, 68, 14, 83, 47, 13,
  77, 16, 59, 43, 80, 79, 83, 30, 52,
  12, 97,  7
]

Am I that unlucky or what? ๐Ÿ˜†

Maybe a better way would be the previousValue to be something like temp array instead and lookup on it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.