Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate random & unique 4 digit codes without brute force

I'm building an app and in one of my functions I need to generate random & unique 4 digit codes. Obviously there is a finite range from 0000 to 9999 but each day the entire list will be wiped and each day I will not need more than the available amount of codes which means it's possible to have unique codes for each day. Realistically I will probably only need a few hundred codes a day.

The way I've coded it for now is the simple brute force way which would be to generate a random 4 digit number, check if the number exists in an array and if it does, generate another number while if it doesn't, return the generated number.

Since it's 4 digits, the runtime isn't anything too crazy and I'm mostly generating a few hundred codes a day so there won't be some scenario where I've generated 9999 codes and I keep randomly generating numbers to find the last remaining one.

It would also be fine to have letters in there as well instead of just numbers if it would make the problem easier.

Other than my brute force method, what would be a more efficient way of doing this?

Thank you!

like image 866
WildWombat Avatar asked Nov 30 '25 08:11

WildWombat


1 Answers

Since you have a constrained number of values that will easily fit in memory, the simplest way I know of is to create a list of the possible values and select one randomly, then remove it from the list so it can't be selected again. This will never have a collision with a previously used number:

function initValues(numValues) {
    const values = new Array(numValues);
    // fill the array with each value
    for (let i = 0; i < values.length; i++) {
        values[i] = i;
    }
    return values;
}

function getValue(array) {
    if (!array.length) {
        throw new Error("array is empty, no more random values");
    }
    const i = Math.floor(Math.random() * array.length);
    const returnVal = array[i];
    array.splice(i, 1);
    return returnVal;
}

// sample code to use it
const rands = initValues(10000);
console.log(getValue(rands));
console.log(getValue(rands));
console.log(getValue(rands));
console.log(getValue(rands));

This works by doing the following:

  1. Generate an array of all possible values.
  2. When you need a value, select one from the array with a random index.
  3. After selecting the value, remove it from the array.
  4. Return the selected value.
  5. Items are never repeated because they are removed from the array when used.
  6. There are no collisions with used values because you're always just selecting a random value from the remaining unused values.
  7. This relies on the fact that an array of integers is pretty well optimized in Javascript so doing a .splice() on a 10,000 element array is still pretty fast (as it can probably just be memmove instructions).

FYI, this could be made more memory efficient by using a typed array since your numbers can be represented in 16-bit values (instead of the default 64 bits for doubles). But, you'd have to implement your own version of .splice() and keep track of the length yourself since typed arrays don't have these capabilities built in.

For even larger problems like this where memory usage becomes a problem, I've used a BitArray to keep track of previous usage of values.


Here's a class implementation of the same functionality:

class Randoms {
    constructor(numValues) {
        this.values = new Array(numValues);
        for (let i = 0; i < this.values.length; i++) {
            this.values[i] = i;
        }
    }
    getRandomValue() {
        if (!this.values.length) {
            throw new Error("no more random values");
        }
        const i = Math.floor(Math.random() * this.values.length);
        const returnVal = this.values[i];
        this.values.splice(i, 1);
        return returnVal;
    }
}

const rands = new Randoms(10000);
console.log(rands.getRandomValue());
console.log(rands.getRandomValue());
console.log(rands.getRandomValue());
console.log(rands.getRandomValue());
like image 188
jfriend00 Avatar answered Dec 01 '25 21:12

jfriend00