javascript - 随机变量结果

标签 javascript spotify

在学校,我们正忙着制作 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/

相关文章:

c - 播放轨道后使用 sp_session_logout 出现段错误

python - Spotify token 交换 ruby​​ 文件托管

spotify - Spotify 的 API 中是否有某种通用 ID 轨道?

javascript - 显示模块模式时的额外括号

javascript - 如何为我的分页添加格式

Javascript 将 JSON 字符串转换为数组的数组

android - 播放单轨Spotify SDK

javascript - AngularJS oauth 端点在 url 中带有哈希(SpotifyAPI)

javascript - 对象可能是 'null' : TypeScript, React useRef & Formik innerRef

javascript - Angular js : disable tab key default behaviour