Joachim: Zufallsauswahl ohne Wiederholung

Beitrag lesen

hi,

sowas?

hth + Gruesse  Joachim

<html>
<head>
<script language="javascript1.2">
var str     =  new Array("lala","blub","foo","bar","ops","fup");
var tempArr =  str;
function randomId (max) {
 return Math.floor(Math.random () * max);
}
function killEl(arr, id) {
 var temp = new Array();
 for(var i in arr) {
  if(i != id) temp[temp.length] = arr[i];
 }
 return temp;
}
function go() {
 if (tempArr.length > 0) {
  var id =  randomId(tempArr.length);
  document.feld.dpl.value =  tempArr[id];
  tempArr =  killEl(tempArr,id);
 } else {
  tempArr =  str;
  document.feld.dpl.value =  "fertig!";
 }
}
</script>
</head>
<body>
<form Name="feld">
    <input type="text" name="dpl" size="30">
    <input type="button" value="go" onclick="go()">
</form>
</body>
</html>