2017-01-03 7 views
1

異なるグループから選択されたアイテムごとにアイテムがランダムに選択されるアイテムのリストを作成したいとします。私はこれで非常に新しいですが、このコードはそうするようです。ランダムに選択されたアイテムのリスト

<html> 
<body> 
<p>Random items from three different groups:</p> 
<script type="text/javascript"> 
<!-- 
document.write('<ol>'); 
    // first group 
    var first_group = new Array(); 
    first_group[0] = "one"; 
    first_group[1] = "two"; 
    first_group[2] = "three"; 
    var i = Math.floor(3*Math.random()) 
    document.write('<li>' + first_group[i]); 
    // second group 
    var second_group = new Array(); 
    second_group[0] = "three"; 
    second_group[1] = "four"; 
    second_group[2] = "five"; 
    var i = Math.floor(3*Math.random()) 
    document.write('<li>' + second_group[i]); 
    // third group 
    var third_group = new Array(); 
    third_group[0] = "five"; 
    third_group[1] = "six"; 
    third_group[2] = "seven"; 
    var i = Math.floor(3*Math.random()) 
    document.write('<li>' + third_group[i]); 
document.write('</ol>'); 
//--> 
</script> 
</body> 
</html> 

それは同じ項目が複数のグループに、同じアイテムが最終リストに複数回出現することはできませんが発生にもかかわらずようにコードを変更することは可能ですか? (たとえば、最初のグループから「3」が選択されている場合、2番目のグループからは選択できません)。そして、最終的なリストをランダムな順序で持つことは可能ですか?他の改善の提案も歓迎します。

+0

http://stackoverflow.com/questions/20292750/unique-randomユニークな値の配列からの値 - javascript http://stackoverflow.com/questions/10688027/50-random-unique-elements-from-an-array-of-1000-elemens – mplungjan

+0

配列の作成すべての3つの結果。最初の1つ後に、この新しい配列に選択された答えが含まれているかどうかをチェックして、3番目のグループに移動しない限り。 – Alan

+0

出力を目的として、3つのグループを持つ理由それらを1つのオブジェクトに統合し、重複を排除するか?Seee http://stackoverflow.com/questions/30025965/merge-duplicate-objects-in-array-of-objects – Sablefoste

答えて

1

これを試してみてください:

<html> 

<body> 
    <p>Random items from three different groups:</p> 
    <script type="text/javascript"> 
     var selectedValues = []; 

     function chooseRandom(array) { 
      // Select random number until it's a number that wasn't selected yet 
      do { 
       var random = Math.floor(array.length * Math.random()); 
       var randomResult = array[random]; 
      } while (selectedValues.indexOf(randomResult) > -1) 

      // Log the selected number so it won't be selected randomly again 
      selectedValues.push(randomResult); 
      return randomResult; 
     } 

     document.write('<ol>'); 
     var first_group = ["one", "two", "three"]; 
     var second_group = ["three", "four", "five"]; 
     var third_group = ["five", "six", "seven"]; 

     document.write('<li>' + chooseRandom(first_group) + '</li>'); 
     document.write('<li>' + chooseRandom(second_group) + '</li>'); 
     document.write('<li>' + chooseRandom(third_group) + '</li>'); 

     // Finalize 
     document.write('</ol>'); 
    </script> 
</body> 

</html> 
+0

'selectedValues'を関数' chooseRandom'のスコープの外に置くことはできません。 – Sablefoste

+0

'selectedValues'はスコープチェーン階層の上位部分で定義されているため、関数' chooseRandom'からアクセスできます –

1

機能が最善の方法になるのJavaScriptのカスタムを使用します。あなたの例に基づいて(あなたは、より複雑な生態系のためにスケールアップする必要がある場合があります。

<script type="text/javascript"> 
<!-- 
var i=j=k=x=0; 

var first_group = ["one", "two", "three"]; 
var second_group = ["three", "four", "five"]; 
var third_group = ["five", "six", "seven"]; 

i = Math.floor(3*Math.random()); 
j = Math.floor(3*Math.random()); 
k = Math.floor(3*Math.random()); 

var okayArray=makeNoDup(first_group,i,second_group,j,third_group,k); 

document.write('<ol>'); 
okayArray.forEach(function(e) { 
    document.write('<li>' + e[x] + '</li>'); 
    x++; 
} 
document.write('</ol>'); 


function makeNoDup(first,i,second,j,third,k){ 
    if(first[i] === second[j]){  
    var newj = Math.floor(3*Math.random()); 
    j= makeNoDup(first, i, second, newj, third, k); 
    } 
    if(first[i] === third[K] || second[j] === third[k]){  
    var newK = Math.floor(3*Math.random()); 
    k= makeNoDup(first, i, second, j, third, newk); 
    } 
    var answer = { 
    first: i, 
    second: j, 
    third: k 
    }; 
    return (answer); 
} 
//--> 
</script> 

キーは、再帰関数makeNoDupある

関連する問題