Javascript 2张随机图像,但不是同一张

Javascript 2张随机图像,但不是同一张,javascript,Javascript,我有一个函数,显示从文件夹中随机选取的两幅图像。有没有可能我可以修改代码,使我不会有相同的图像两倍的结果 提前谢谢 var theImages = new Array() theImages[0] = 'img/dyptichs/f-1.jpg' theImages[1] = 'img/dyptichs/f-2.jpg' theImages[2] = 'img/dyptichs/f-3.jpg' theImages[3] = 'img/dyptichs/f-4.jpg' theImages[4

我有一个函数,显示从文件夹中随机选取的两幅图像。有没有可能我可以修改代码,使我不会有相同的图像两倍的结果

提前谢谢

var theImages = new Array()

theImages[0] = 'img/dyptichs/f-1.jpg'
theImages[1] = 'img/dyptichs/f-2.jpg'
theImages[2] = 'img/dyptichs/f-3.jpg'
theImages[3] = 'img/dyptichs/f-4.jpg'
theImages[4] = 'img/dyptichs/f-5.jpg'

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var WI1 = Math.round(Math.random()*(p-1));
var WI2 = Math.round(Math.random()*(p-2));

function showImage1(){
document.write('<img src="'+theImages[WI1]+'">');
}
function showImage2(){
document.write('<img src="'+theImages[WI2]+'">');
}
var theImages=newarray()
图像[0]=“img/dyptichs/f-1.jpg”
图像[1]=“img/dyptichs/f-2.jpg”
图像[2]=“img/dyptichs/f-3.jpg”
图像[3]=“img/dyptichs/f-4.jpg”
图像[4]=“img/dyptichs/f-5.jpg”
var j=0
var p=图像长度;
var preBuffer=新数组()
对于(i=0;i
您可以执行以下操作:

var WI1 = Math.round(Math.random()*(p-1));
var WI2 = Math.round(Math.random()*(p-1));
while (WI2 === WI1) {
    WI2 = Math.round(Math.random()*(p-1));
}

我们不断生成一个新数字,直到它与WI1不同,确保它是唯一的。

您可以这样做:

var WI1 = Math.round(Math.random()*(p-1));
var WI2 = Math.round(Math.random()*(p-1));
while (WI2 === WI1) {
    WI2 = Math.round(Math.random()*(p-1));
}
var WI1 = Math.floor(Math.random()*p);
var WI2 = Math.floor(Math.random()*(p-1));
if (WI2 >= WI1) {
  WI2 += 1;
}
我们不断生成一个新的数字,直到它与WI1不同,确保它是唯一的

var WI1 = Math.floor(Math.random()*p);
var WI2 = Math.floor(Math.random()*(p-1));
if (WI2 >= WI1) {
  WI2 += 1;
}
使用floor而不是round并减去1,因为round可以减少两倍获得第一个或最后一个元素的机会

在这种情况下,if技巧略优于循环,尽管循环更容易应用于更复杂的情况

使用floor而不是round并减去1,因为round可以减少两倍获得第一个或最后一个元素的机会


在这种情况下,if技巧比循环稍微好一点,尽管循环更容易应用于更复杂的情况。

我个人的处理方法是随机化数组,然后只获取前2个条目。这样,您仍然可以随机选择2,但您保证不会得到相同的2

var theImages = new Array()

theImages[0] = 'img/dyptichs/f-1.jpg'
theImages[1] = 'img/dyptichs/f-2.jpg'
theImages[2] = 'img/dyptichs/f-3.jpg'
theImages[3] = 'img/dyptichs/f-4.jpg'
theImages[4] = 'img/dyptichs/f-5.jpg'

var randomImages = theImages
    .concat()
    .sort(function () {

        return Math.random() > 0.5
            ? 1
            : -1;

    })
    .slice(0, 2);

function showImage1() {
    document.write('<img src="' + randomImages[0] + '">');
}

function showImage2() {
    document.write('<img src="' + randomImages[1] + '">');
}
var theImages=newarray()
图像[0]=“img/dyptichs/f-1.jpg”
图像[1]=“img/dyptichs/f-2.jpg”
图像[2]=“img/dyptichs/f-3.jpg”
图像[3]=“img/dyptichs/f-4.jpg”
图像[4]=“img/dyptichs/f-5.jpg”
var randomImages=图像
.concat()
.sort(函数(){
返回Math.random()>0.5
1.
: -1;
})
.切片(0,2);
函数showImage1(){
文件。写(“”);
}
函数showImage2(){
文件。写(“”);
}

编辑:包括原始数组以获得完整的解决方案

我个人的处理方式是将数组随机化,然后只抓取前2个条目。这样,您仍然可以随机选择2,但您保证不会得到相同的2

var theImages = new Array()

theImages[0] = 'img/dyptichs/f-1.jpg'
theImages[1] = 'img/dyptichs/f-2.jpg'
theImages[2] = 'img/dyptichs/f-3.jpg'
theImages[3] = 'img/dyptichs/f-4.jpg'
theImages[4] = 'img/dyptichs/f-5.jpg'

var randomImages = theImages
    .concat()
    .sort(function () {

        return Math.random() > 0.5
            ? 1
            : -1;

    })
    .slice(0, 2);

function showImage1() {
    document.write('<img src="' + randomImages[0] + '">');
}

function showImage2() {
    document.write('<img src="' + randomImages[1] + '">');
}
var theImages=newarray()
图像[0]=“img/dyptichs/f-1.jpg”
图像[1]=“img/dyptichs/f-2.jpg”
图像[2]=“img/dyptichs/f-3.jpg”
图像[3]=“img/dyptichs/f-4.jpg”
图像[4]=“img/dyptichs/f-5.jpg”
var randomImages=图像
.concat()
.sort(函数(){
返回Math.random()>0.5
1.
: -1;
})
.切片(0,2);
函数showImage1(){
文件。写(“”);
}
函数showImage2(){
文件。写(“”);
}

编辑:包含完整解决方案的原始数组

如果WI1为4,则索引越界;如果WI1为4,则索引越界;如果WI1为4,则索引越界;如果WI1为4,则索引越界;如果random为1,则索引越界;但是,您会将数组放在哪里?@Federico抱歉,我不理解这个问题
randomImages
是数组,它在当前范围内(在我看来,它类似于全局范围)。这回答了你的问题吗?是的,但是图像在一个文件夹中,这里没有指定。使用随机比较器排序不是洗牌序列的正确方法,因为它不会给出一致的随机结果。@riv你完全正确。我想要一个简单的解决方案来快速解释,但是有更好的混合解决方案。例如,在这个答案中,你会把数组放在哪里?@Federico抱歉,我不明白这个问题
randomImages
是数组,它在当前范围内(在我看来,它类似于全局范围)。这回答了你的问题吗?是的,但是图像在一个文件夹中,这里没有指定。使用随机比较器排序不是洗牌序列的正确方法,因为它不会给出一致的随机结果。@riv你完全正确。我想要一个简单的解决方案来快速解释,但是有更好的混合解决方案。例如在这个答案中