在学校,我们正忙着制作 Spotify 应用程序。我目前正在制作一个应用程序,可以从 LastFM 获取当前正在演奏的艺术家的图像。我显示了三个随机图像。我现在正在尝试确保这 3 个随机图像不能相同。
这就是我现在所拥有的:
var randno = Math.floor ( Math.random() * artistImages.length );
var randno2 = Math.floor ( Math.random() * artistImages.length );
var randno3 = Math.floor ( Math.random() * artistImages.length );
现在我想确保它们不一样。有人可以帮我解决这个问题吗?
最佳答案
使用 while loop :
var randno = Math.floor ( Math.random() * artistImages.length );
var randno2 = Math.floor ( Math.random() * artistImages.length );
while (randno2==randno)
{
randno2 = Math.floor ( Math.random() * artistImages.length );
}
var randno3 = Math.floor ( Math.random() * artistImages.length );
while (randno3==randno || randno3==randno2)
{
randno3 = Math.floor ( Math.random() * artistImages.length );
}
关于javascript - 随机变量结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9941274/