Javascript(html)Math.random()范围不工作

Javascript(html)Math.random()范围不工作,javascript,html,Javascript,Html,我想在“lowestnum”和“highestnum”之间生成一个随机数。它们是用户的输入 我在中使用了此代码,但生成的数字不在范围内 var secret = Math.floor(Math.random()*(highestnum-lowestnum+1)+lowestnum); 例如:我输入了lower=4,highest=6,secret应该是4/5/6。但实际上,生成的数字是0/1/2。我发现这是因为6-4是2,它只会生成0到2,这是数字之间的差值,但不是4到6之间的任何数字 编辑

我想在“lowestnum”和“highestnum”之间生成一个随机数。它们是用户的输入

我在中使用了此代码,但生成的数字不在范围内

var secret = Math.floor(Math.random()*(highestnum-lowestnum+1)+lowestnum); 

例如:我输入了lower=4,highest=6,secret应该是4/5/6。但实际上,生成的数字是0/1/2。我发现这是因为6-4是2,它只会生成0到2,这是数字之间的差值,但不是4到6之间的任何数字

编辑!更新了更清晰的问题


这些是它在html中的内容

<input id="highestnum" type="text" name="highestnum"> ```

and inside javascript is this 
```var lowestnum = document.getElementById("lowestnum").value; 
var highestnum = document.getElementById("highestnum").value; 
var secret = Math.floor(Math.random()*(highestnum-lowestnum+1)+lowestnum);```
```
javascript的内部是这样的
```var lowestnum=document.getElementById(“lowestnum”).value;
var highestnum=document.getElementById(“highestnum”).value;
var secret=Math.floor(Math.random()*(highestnum-lowerstnum+1)+lowerstnum)```
试试这个

函数randomNumber()
{
var lowestnum=parseInt(document.getElementById(“lowestnum”).value);
var highestnum=parseInt(document.getElementById(“highestnum”).value);
secret=Math.floor(Math.random()*(Math.abs(highestnum-lowerstnum)+1)+lowerstnum);
控制台日志(机密);
}
window.onload=函数(){
document.addEventListener('keyup',函数(事件){
var lowestnum=document.getElementById(“lowestnum”);
var highestnum=document.getElementById(“highestnum”);
if(event.target==lowerstnum | | event.target==highestnum){
if(lowestnum.value.length>0&&highestnum.value.length>0){
document.getElementById(“按钮”).removeAttribute(“禁用”);
}
否则{
document.getElementById(“按钮”).setAttribute(“已禁用”、“为真”);
}
}
},假);
}

根据生成

获取介于0(包含)和1(排除)之间的随机数:

function getRandom() {
  return Math.random();
}
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive 
}
获取两个值之间的随机数:

function getRandom() {
  return Math.random();
}
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive 
}
获取两个值之间的随机整数:

function getRandom() {
  return Math.random();
}
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive 
}
获取两个值之间的随机整数,包括:

function getRandom() {
  return Math.random();
}
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1) + min); //The maximum is inclusive and the minimum is inclusive 
}

HTML中的
lowerstnum
highestnum
输入在哪里?您已经用它们的值覆盖了11和63的参数。另外,如果
min
是一个字符串,
+min
将与其连接,而不是添加。这些是它的内部html
和内部javascript是这个
var lowestnum=document.getElementById(“lowestnum”).value;var highestnum=document.getElementById(“highestnum”).value;var secret=Math.floor(Math.random()*(highestnum-lowerstnum+1)+lowerstnum)最小值和最大值为strings@javadead在JavaScript中不能直接取整数值,因为它们被认为是一个字符串,所以需要使用<代码> JS PARSETIN()/<代码>函数将其转换成整数。使用10和12hi,谢谢您的帮助,但范围似乎不起作用。我得到的答案仍然不对。生成的随机数不在我输入的范围内,事实上,生成的数在我输入的数的差值内。请给出@javadead的一些示例例如:我输入的最低值=4,最高值=6,机密值应为4/5/6。但实际上,生成的数字是0/1/2。我发现这是因为6-4是2,它只会生成0到2,这是数字之间的差异,但不是4到6之间的任何数字。嗨,我刚刚看到了带有“parseint”的更新版本,它可以工作!!非常感谢。