我想找到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