如何显示 URL 中带有随机数的图像?

How can I display an image from a URL with a random number in it?

我正在尝试使用 PubChem 制作某种化合物生成器。我已经成功地为网站生成了一个带有随机数的 link(例如 https://pubchem.ncbi.nlm.nih.gov/compound/123456), as well as a link to the image (e.g. https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=123456&t=l)。但是,当我尝试将随机图像 link 嵌入到文档中时,它并没有出现。在编程方面,我只是初学者。

var lower = 0;
var upper = 100000000;
var rand = (Math.floor(Math.random() * (upper - lower)) + lower)
var url = "https://pubchem.ncbi.nlm.nih.gov/compound/" + rand
var img = "https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=" + rand + "&t=l"
document.write("<a href=" + url + ">" + url + "</a>");
document.write("</br><a href=" + img + ">" + rand + "</a>");
document.write("</br><img src=" + img + "alt=" + rand + ">")

rand 保存一个整数。首先尝试将其更改为字符串:

var rand = (Math.floor(Math.random()*(upper-lower))+lower).toString()

javascript 的 HTML 输出错误。看看最后一行javascript:

var lower = 0;
var upper = 100000000;
var rand = (Math.floor(Math.random() * (upper - lower)) + lower);
var url = "https://pubchem.ncbi.nlm.nih.gov/compound/" + rand;
var img = "https://pubchem.ncbi.nlm.nih.gov/image/imgsrv.fcgi?cid=" + rand + "&t=l"
document.write('<a href="' + url + '">' + url + '</a>');
document.write('</br><a href="' + img + '">' + rand + '</a>');
document.write('</br><img src="' + img + '" alt="' + rand + '">');