我想找到100到1000之間的10個隨機數,如果有任何數字是相等的,那麼我需要讓電腦選擇一個不同的隨機數。有誰知道如何做到這一點?如何生成不相等的隨機數
<script type="text/javascript">
var numbers=[];
var max=110;
var min=100;
numbers [1]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [2]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [3]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [4]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [5]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [6]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [7]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [8]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [9]= Math.floor(Math.random() * (max - min + 1)) + min;
numbers [10]= Math.floor(Math.random() * (max - min + 1)) + min;
do (numbers[2]);
while (numbers[1]===numbers[2]);
do (numbers[3]);
while (numbers[1] == numbers[3])
(numbers[2] == numbers[3]);
for (i in numbers){
document.writeln("<br>"+numbers[i]);
}
</script>
該使用對象來模擬一組,看到http://www.javascriptexamples.org/2011/01/17/how -to-implement -a-set-in-javascript/ – user3557327 2014-11-04 18:12:18