2

I'm in the early stages of learning JavaScript and I had thought of making a lottery number generator for practice. This is what I have so far (z is for the total lottery balls, and y for the number of digits to be generated):

function generateRandomNumbers(z,y){
            var resultArray = [];
            var output = "";

            if(isNaN(z) || isNaN(y)){
                return "You have entered an invalid value";
            }else{
            for(x=0; x<y; x++){
                var result = Math.ceil(Math.random()*z);
                resultArray.push(result);

                output += resultArray[x]+ "\n";
                }
            }   
            return output;
        }

The problem is, this doesn't prevent duplicates from occurring. What could be a good way to ensure that the array's content doesn't contain any duplicates? I apologize if this has already been asked before.

wong1
  • 59
  • 1
  • 6
  • 1
    Put them in a list and check if the number is in the list already? – Roope Aug 27 '15 at 16:06
  • possible duplicate of [Creating a random number generator in jscript and prevent duplicates](http://stackoverflow.com/questions/5556263/creating-a-random-number-generator-in-jscript-and-prevent-duplicates) – BSMP Aug 27 '15 at 16:07
  • Part of this question seems to be a duplicate as well: http://stackoverflow.com/questions/25975579/javascript-lottery-numbers – BSMP Aug 27 '15 at 16:08

3 Answers3

2

As its lottery, the possible numbers are contiguous. You can create a list that holds all possible number, then use .splice to take one from them each time, until you take enough numbers:

function generateRandomNumbers(z,y){
    // You can't take more, or you can set y = z;
    if (y > z) {
      return [];
    }
  
    var numbers = [];
    var result = [];
    var i;
    // Create list.
    for (i = 1; i <= z; ++i) {
        numbers.push(i);
    }

    var random;
    // Take a random number from list each time.
    for (i = 0; i < y; ++i) {
        random = Math.floor(Math.random() * z);
      
        // Put the randomly take number into result.
        result.push(numbers.splice(random, 1)[0]);
      
        // As we take one number from the list, remember to decrease the possible range by 1.
        --z;
    }
    return result;
    // If you want  a string form
    // return result.join('\n');
}

console.log(generateRandomNumbers(49, 6))
fuyushimoya
  • 9,715
  • 3
  • 27
  • 34
  • 2
    I believe you mean "contiguous". – Scott Sauyet Aug 27 '15 at 16:17
  • Hi, thanks for answering, this solves my problem, @fuyushimoya. I would just like to clarify what [0] is in this line of code: result.push(numbers.splice(random, 1)[0]); As I understand, this line takes the spliced number from the list and pushes it to the result array, but I'm clueless to what [0] is. – wong1 Aug 28 '15 at 00:40
  • splice return what was cut from array, in array form. Even if we only cut one , the only item will be in an array, so we need to use [0] to get that item from the returned array. – fuyushimoya Aug 28 '15 at 01:05
0

I suggest using jQuery...

var uniqueNum= [];
$.each(num, function(i, el){
   if($.inArray(el, uniqueNum) == -1) 
         uniqueNum.push(el);
});

nice and easy =]

xcoder
  • 1,336
  • 2
  • 17
  • 44
0

You can test if the number that was just generated is already in resultArray.

If so, then just try to loop again for this number

for(var x = 0 ; x < y ; x++){
    var result = Math.ceil(Math.random()*z);

    if (resultArray.indexOf(result) > -1){
        x--;
    }
    else{
        resultArray.push(result);
    }
}

return resultArray.join("\n");

Also you have to check for y <= z because you can't generate more unique numbers than the range [0 - [z.

Hacketo
  • 4,978
  • 4
  • 19
  • 34