I am trying to set up a function that, for example, will return 10 numbers between 1 and 5 or 6 and 10 (1/6 and 5/10 included with duplicates) But I keep getting numbers between 0 and 4.
function potato(min, max, times) {
const randoms = []
min = document.getElementById("num-min").value // 1/6
max = document.getElementById("num-max").value // 5/10
times = document.getElementById("num-range").value // 10
for (let i = 0; i < times; i++) {
randoms.push(Math.floor(Math.random() * (max - min + 1) + min))
}
console.log(randoms)
}
<div class="col-8 vh-100">
<div class="row vh-100">
<div class="d-flex flex-column vh-100 mt-5">
<body>
<div class="container mt-3">
<h2 class="justify-content-center">Random Number Generator</h2>
<form>
<div class="mb-3 mt-3">
<label for="num-range">Range:</label>
<input type="number" class="form-control" id="num-range" name="num-range">
</div>
<div class="mb-3">
<label for="num-min">Min:</label>
<input type="number" class="form-control" id="num-min" name="num-min">
</div>
<div class="mb-3">
<label for="num-max">Max:</label>
<input type="number" class="form-control" id="num-max" name="num-max">
</div>
<button type="button" class="btn btn-primary" onclick="potato()">Submit</button>
</form>
</div>
<div class="container mt-3">
<h2 class="justify-content-center">Result</h2>
<div class="mb-3 mt-3">
<textarea class="form-control" type="number" id="results" value="" rows="5" readonly></textarea>
</div>
</form>
</div>
</body>
</div>
</div>
</div>
The min, max, times variables are pulled from input fields on my webpage and the submit button calls the function
https://i.imgur.com/yQGoPXj.png (the input field)
The output I got after 5 runs on each was
1-5
(10) [4, 2, 0, 0, 2, 1, 2, 3, 0, 3]
(10) [4, 2, 4, 3, 3, 1, 2, 1, 3, 4]
(10) [3, 4, 1, 2, 1, 4, 0, 0, 0, 3]
(10) [2, 2, 2, 2, 4, 3, 0, 0, 4, 4]
(10) [4, 2, 0, 2, 4, 3, 2, 0, 2, 3]
6-10
(10) [4, 4, 0, 3, 1, 4, 4, 3, 4, 3]
(10) [4, 3, 3, 2, 2, 3, 0, 3, 4, 0]
(10) [3, 3, 4, 1, 0, 1, 1, 1, 2, 1]
(10) [1, 4, 1, 3, 0, 1, 4, 0, 0, 1]
(10) [1, 1, 4, 4, 2, 1, 1, 1, 4, 4]
I added the +1 here and it went from 2 below to one below the max
randoms.push(Math.floor(Math.random() * (max - min + 1) + min))
I also attempted to randomize the numbers as an array but that only shuffled the array I had and didn't allow for duplicates
function potato(min, max, times) {
min = document.getElementById("num-min").value
max = document.getElementById("num-max").value
times = document.getElementById("num-range").value
countArr = Array.from({length: max}, (_, i) => i + 1)
for (let i = countArr.length -1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
let k = countArr[i]
countArr[i] = countArr[j]
countArr[j] = k
}
numResults = countArr.slice(0, times)
console.log(numResults.join(', '))
}