mardi 29 mars 2016

Javascript random numbers no duplicates not in specific list

I'm trying to get a list of 10 random numbers between 0-30 with Javascript.
So far so good, but i've another list with specific numbers (0,5,10,11,12,13,15,16,17,18,20,22,23).
Only max. 4 numbers of this list are allowed in the random array. This makes me crazy because i've no idea to combine this.

I'm grateful for any help. Thank you.

Here is my code:

<script language="JavaScript" type="text/javascript">
<!--    

function random(min, max, i, filter) {
  var array = {};
  var zufall = [];
  while( i ) {

    var rnd = Math.floor(Math.random() * (max - min + 1)) + min;
    if( !array[rnd] )  {
      array[rnd] = true;
      i--;
      zufall.push(rnd);
    }
  }
  return zufall;
}

var filter = new Array(0,5,10,11,12,13,15,16,17,18,20,22,23);

console.log("Random: " + random(0,30,10,filter) );

//-->
</script>




Aucun commentaire:

Enregistrer un commentaire