-1

I just made a button which gives me a random number from 1 to 57, and depending on the number given, I obtain a value associated to the number:

function coches() {
  var x = Math.floor((Math.random() * 57) + 1);
  if (x <= 3) {
    document.getElementById("coche").innerHTML = "Audi " + x;
  } else if (x <= 9) {
    document.getElementById("coche").innerHTML = "BMW " + x;
  } else if (x <= 17) {
    document.getElementById("coche").innerHTML = "Mercedes " + x;
  } else {
    document.getElementById("coche").innerHTML = "Seat " + x;
  }
}
<p id="coche"></p>
<input type="button" value="Click aqui!" onclick="coches()">

The problem is that now I need two things:

1.- The random number must depend on the probability of each car (I don't know If I explainded myself good, but I'll add an image above)

2.- When a random number appears, it should be removed from the list

Here is the image trying to explain it:

enter image description here

Preston S
  • 2,751
  • 24
  • 37
Nytz
  • 3
  • 2
  • 2
    first create an array of all numbers and the after randon use indexOf to remove it from the array. – Gildas.Tambo Dec 18 '14 at 16:15
  • 1
    @Tambo That would solve only the #2. – Teemu Dec 18 '14 at 16:17
  • 2
    Why not make an array of ints 1 to 57 and then shuffle it? You don't need to randomly generate a number each button push, just randomly generate the entire list and iterate over it on button push. This solves both problems. – Preston S Dec 18 '14 at 16:22
  • @PrestonS Looks like OP wants to bind a certain range of numbers to the make, in that case "a guided distribution" can't be reached in a way you've suggested. If the numbers are not important, then it'll work. – Teemu Dec 18 '14 at 16:32
  • @Teemu See my answer below, it fulfills all the requirements. – Preston S Dec 18 '14 at 16:34
  • @PrestonS Indeed it is. One cosmetic change should be done though. OP wants numbers from 1 to 57, you have 0 - 56. – Teemu Dec 18 '14 at 16:38

2 Answers2

2

You don't need to randomly generate the number on each button push, just create them all ahead of time and iterate over them when you push the button.

var idx = 0;
var randomNums = [];

for (var i = 1; i <= 57; i++) {
  randomNums.push(i);
}

shuffle(randomNums);

//http://stackoverflow.com/a/2450976/2464634
function shuffle(array) {
  var currentIndex = array.length,
    temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

function coches() {
  if (idx < randomNums.length) {
    if (randomNums[idx] <= 3) {
      document.getElementById("coche").innerHTML += "Audi " + randomNums[idx];
    } else if (randomNums[idx] <= 9) {
      document.getElementById("coche").innerHTML += "BMW " + randomNums[idx];
    } else if (randomNums[idx] <= 17) {
      document.getElementById("coche").innerHTML += "Mercedes " + randomNums[idx];
    } else {
      document.getElementById("coche").innerHTML += "Seat " + randomNums[idx];
    }
    
    document.getElementById("coche").innerHTML += "<br/>"

    idx++;
  }
}
<p id="coche"></p>
<input type="button" value="Click aqui!" onclick="coches()">
Preston S
  • 2,751
  • 24
  • 37
  • Thank you very much, but how about If I want to push them out from a database? – Nytz Dec 18 '14 at 16:52
  • 1
    @Nytz, you should ask that as another question and tag it appropriately. You will need to include a lot more detail to get a proper answer. Link the question here after you create it and I will take a look. – Preston S Dec 18 '14 at 16:55
0

First of all, from which list should it disappear? If you mean that it should no longer pop up when randomized then this is not possible with Math.random the way you wrote it at this moment.

You should keep a list of numbers. for example:

//create an array and fill it with numbers zero to 57
var list = [];
for (var i = 0; i < 57; ++i)
{
    list.push(i);
}

if (list.length > 0)
{
    var x = Math.floor((Math.random() * list.length) + 1);
    var randomNumber = list.splice(x, 1); //delete element from array and return it.
    //now the length of the array is reduced by one.
}

Every time when a randomNumber is produced it will refer to a number in the list array. However previously rendered number will not be reused.

Mouser
  • 13,132
  • 3
  • 28
  • 54