Unique Random Number Generator Javascript

16,838

Solution 1

once it actually hits a number that's the same

It never should. Such algorithms take longer the longer they run. You should take a different approach:

Put all possible numbers into a pool. Once you draw a number, remove it from the pool. Just like it's done in real life.

var pool = [1, 2, 3, 4, 5];
var getNumber = function () {
    if (pool.length == 0) {
        throw "No numbers left";
    }
    var index = Math.floor(pool.length * Math.random());
    var drawn = pool.splice(index, 1);
    return drawn[0];
};

Solution 2

I would rather do it with something like this:

http://jsfiddle.net/YC58s/

generate = function(length)
{
    var arr = [];
    var n;
    for(var i=0; i<length; i++)
    {
        do
            n = Math.floor(Math.random()*20+1);
        while(arr.indexOf(n) !== -1)

       arr[i] = n;
    }

    return arr;
}
Share:
16,838
Trent Braswell
Author by

Trent Braswell

Updated on June 09, 2022

Comments

  • Trent Braswell
    Trent Braswell almost 2 years

    I'm trying to make a bingo game for fun. I've looked in a lot of places for a unique generator but I can't seem to find one. I've tried to make my own,but once it actually hits a number that's the same it does an infinite loop. I've tried a simple code that in theory should work but for some reason things pass through. What can I do!?

    var bc = [];
    for (var i = 0; i < 5; i++) {
      var r = Math.floor(Math.random()*20+1) + 0;
      if(!(r in bc)){
        bc.push(r);     
        }
        else
        {
        i--;
        }
    }
    ____________________________________________
    ____________________________________________
    ____________________________________________
    b1=0;
    b2=0;
    b3=0;
    b4=0;
    b5=0;
    var bc = [b1,b2,b3,b4,b5]
    var bnc = function(){
        var n = Math.floor(Math.random() * 5+1)+0;
        var n2 = Math.floor(Math.random() * 5+1)+0;
        b1 = n;
        var a1 = true;
        var as = false;
        while(a1){
            var c = n;
            if(c===b1||c===0 ||as!==false){
            c = n2;
            as=true;
            }
            if(c===b1||c===0&&as===true){
            c = n;
            as=false;
            }
                if(c!=b1){
                b2 = c;
                a1 = false;
                a2 = true;
            }
        }
    };
    bnc();
    console.log("new1");
    console.log(b1,b2,b3,b4,b5);
    //_______________________________________
    var bnc2 = function(){
        var n = Math.floor(Math.random() * 5+1)+0;
        var n2 = Math.floor(Math.random() * 5+1)+0;
        var a1 = true;
        var as = false;
        while(a1){
            var c = n;
            if(c===b1||c===b2||c===0&&as===false){
            c = n2;
            as=true;
            }
            if(c===b1||c===b2||c===0&&as===true){
            c = n;
            as=false;
            }
            if(c!=b1&&c!=b2){
                b3 = c;
                console.log("made it 1");
                a1 = false;
            }
        }
    };
    bnc2();
    console.log("new2");
    console.log(b1,b2,b3,b4,b5);
    
    • apelsinapa
      apelsinapa about 11 years
      It wouldn't be entirely wrong with some comments and/or description of what the functions is supposed to do since its so much code. You want to generate a sequence of unique random? It seems to be alot of code just for that...
    • Kane
      Kane about 11 years
      You could easily adapt one of these answers if you need a unique random number: stackoverflow.com/questions/105034/…
    • nnnnnn
      nnnnnn about 11 years
      Hint about your first for loop: the in operator doesn't do what you think it does (it tests property names, not property values).
    • DShah
      DShah almost 11 years
      Below link shows how to generate unique number with defined quantity of numbers. It is really simple logic at Javascript layer. blog.chrometaphore.com/2012/01/17/…
  • Trent Braswell
    Trent Braswell about 11 years
    Could you explain how this works. I get the loop, the return, the vars, but the while statement. How does that work, why does it not make repeats?
  • apelsinapa
    apelsinapa about 11 years
    @TrentBraswell the while loop is a do/while. Which means it will run the inline statement and setting the n to a random number, before checking the criterium. The criterium evaluates if there is such a value in the array. indexOf(n) returns the integer key for the value of n if it exists in the array, otherwise it returns -1. Thus, running the loop again if indexOf does not return -1.