-1

I am trying to create a function that will generate a random number and store it in an array so the first click will send the random number to the index[0] click 2 to index [1] ect. I need to be able to compare the number with the one before (index [4] with index [3].I am sure the answer is right in front of me but i cannot find a solution. Any help would be fantastic

for(i = 0;i < 12;i++) {
             var random_number = Math.floor(Math.random() * 12);   
            var myArray = [];
            myArray.push(random_number);
            console.log(myArray.length);

            document.getElementById("catchme").innerHTML = random_number;
              }
            });

http://codepen.io/kingnarwal/pen/BzjRjq?editors=1111

1 Answers1

0
var myArray = [];
for(var x = 0, maxValue = 12, random_number; x < 12; x++) {
    do {
        random_number = Math.floor(Math.random() * maxValue );
    } while(random_number == myArray[x - 1]);//Check if the number is the same value
    myArray.push(random_number);
}
console.log(myArray);

This does not generate an array with random unique numbers since you're only checking the item before the current item.

To make values unique in whole array:

var myArray = [];
for(var x = 0, maxValue = 12; x < maxValue; x++) {
    myArray.splice(Math.floor(Math.random() * myArray.length), 0, x);
}
console.log(myArray);

Above is a bit hackish method since it uses splice with an random index :P Keep in mind that above method is FAR from random.

A more random method would be:

var myArray = [];
for(var x = 0, x < 12; x++) {
    myArray.push(x);
}
shuffle(myArray);
console.log(myArray);

You can use an array shuffle method from here: How to randomize (shuffle) a JavaScript array?

Community
  • 1
  • 1
seahorsepip
  • 4,519
  • 1
  • 19
  • 30
  • This could in the worst case generate an endless loop. Simply filling an array with all possible values, and then shuffling that array is usually a preferable approach. – CBroe Jun 09 '16 at 19:55
  • What about the splice approach? It's randomly placing each array item when added to to the array. Of course it'as also possible to write a shuffle function and just shuffle a array with all numbers in it. – seahorsepip Jun 09 '16 at 19:58
  • @CBroe It's technically impossible to create an endless loop with both code, logically speaking. – seahorsepip Jun 09 '16 at 19:59
  • No, it is not impossible - Math.random _could_ return the exact same number each time. – CBroe Jun 09 '16 at 20:03
  • @CBroe Fair point though the chance of that happening is almost zero if not zero, I assume the psuedo random function in Math.random() remembers the previous random numbers generated to generate more randomly a number as in most programming languages is the case, in C# the random generator is even a object due to that fact :P – seahorsepip Jun 09 '16 at 20:06
  • _"I assume the psuedo random function in Math.random() remembers the previous random numbers generated to generate more randomly a number"_ - no, that would be _less_ random. Many people have a wrong understanding of what true "randomness" _is_ - if you throw a dice five times, and it comes up 6 every time - then that _is_ perfectly random. (Only for a _much_ larger number of throws the average should be different of course - otherwise it is rather likely a faulty dice.) – CBroe Jun 09 '16 at 20:10