0

I have a project i'm working on.

I am to basically recreate lotto on the client side using javascript to generate 6 random numbers plus a bonus ball. as we all know, lotto numbers cannot be the same. this is where my question comes in.

Is it possible to remove a number that has been generated from being available in the next time round in the loop? This would make the function completely random. or do I need to still compare the number with the others in the array using indexOf?

for example, is the following possible?,

the first number that generates is 25, the function then removes that number from being able to come up again. so on...

Here is my js code,

    function play(){
    numbersArray = [];
    for (i=0; i<=6;){
        n = Math.floor(Math.random()*40)+1;
        a = numbersArray.indexOf(n);
        if ( a == "-1"){
            numbersArray[i] = n;
            i++;
            var ballId = "ball"+i;
            if( i != "7"){
                document.getElementById(ballId).innerHTML = '<p>'+ n +'</p>';   
            } else {
                document.getElementById("bonus").innerHTML = '<p>'+ n +'</p>';
            } 
        } //end of if
    }//end of for loop
}//end of play function
Francois
  • 3,050
  • 13
  • 21
websiteninja
  • 36
  • 1
  • 4
  • 5
    Generate all the numbers the can possibly be on a ball in an array of that same length. Then shuffle the array and take the first seven entries. Think of it as shuffling a deck of cards and then taking the first 7 cards. Or, think of it as having a glass tank of lotto balls that are bouncing around, and take the first 7 that drop out of a hole :) – Pointy May 12 '14 at 21:35
  • Take a look at: http://wcetdesigns.com/tutorials/2012/09/03/array-rand-js.html – Barbara Laird May 12 '14 at 21:51
  • possible [duplicate](http://stackoverflow.com/a/1527820/2172543) – Marcel May 12 '14 at 21:55
  • See [here](http://stackoverflow.com/questions/19351759/javascript-random-number-out-of-5-no-repeat-until-all-have-been-used/19351899#19351899) for a `uniqueRandom()` function. – jfriend00 May 12 '14 at 22:00

1 Answers1

1

You need to create an object, in this case you could use an array, that holds all the possible numbers that can appear on the ball, we'll cal it n. Then you can use a while loop to keep picking numbers from that array, and splice/remove that specific number from the array on every iteration.

function play(n) {

  var picks = [];
  // Store possibilities in the numbersArr array
  var numbersArr = [];

  // n is the max number you can choose to appear on a ball
  for ( var i = 0; i < n; i++ ) { 
    numbersArr.push(i);
  }

  while (picks.length < 7){
     var randomIndex = Math.floor(Math.random() * numbersArr.length);
     picks.push(numbersArr[randomIndex]);
     numbersArr.splice(randomIndex, 1);
  }

  return picks;
}
JoshSGman
  • 529
  • 2
  • 6
  • 16
  • This is not really the best way to do things. It's better to shuffle the array with a Fisher-Yates shuffle and then just take the first 7 elements. – Pointy May 13 '14 at 00:02
  • Can you please explain how it's better? I'm curious to know what the advantages are of the Fisher-Yates shuffle in this instance. Ta – websiteninja May 13 '14 at 02:00