I want random numbers at every 5 numbers in range of 315. So the result should be like:
2 (from 1 to 5)
7 (from 6 to 10)
11 (from 11 to 15)
16 (from 16 to 20)
24 (from 21 to 25)
29 (from 26 to 30)
33 (from 31 to 35)
36 (from 36 to 40)....and so on till 315.
For more clear view I'm providing 3 example sequences:
- 1,6,14,20,23,27,31,39...
- 3,8,15,16,22,29,32,37...
- 1,10,14,19,21,30,33,36...
I've tried below code:
<button onclick="document.getElementById('demo').innerHTML = getRndInteger(1,5); document.getElementById('demo1').innerHTML = getRndInteger(6,10)">Click Me</button>
<p id="demo"></p>
<p id="demo1"></p>
<script>
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
</script>
but for this I need to create for all 63 intervals. anybody knows how to simplify this? I need every number at 5 numbers in series till 315. Can anyone help me?
I want random numbers at every 5 numbers in range of 315. So the result should be like:
2 (from 1 to 5)
7 (from 6 to 10)
11 (from 11 to 15)
16 (from 16 to 20)
24 (from 21 to 25)
29 (from 26 to 30)
33 (from 31 to 35)
36 (from 36 to 40)....and so on till 315.
For more clear view I'm providing 3 example sequences:
- 1,6,14,20,23,27,31,39...
- 3,8,15,16,22,29,32,37...
- 1,10,14,19,21,30,33,36...
I've tried below code:
<button onclick="document.getElementById('demo').innerHTML = getRndInteger(1,5); document.getElementById('demo1').innerHTML = getRndInteger(6,10)">Click Me</button>
<p id="demo"></p>
<p id="demo1"></p>
<script>
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
</script>
but for this I need to create for all 63 intervals. anybody knows how to simplify this? I need every number at 5 numbers in series till 315. Can anyone help me?
Share Improve this question edited Jan 18, 2021 at 11:45 Khushbu Vaghela asked Jan 18, 2021 at 10:25 Khushbu VaghelaKhushbu Vaghela 6192 gold badges5 silver badges19 bronze badges 10- what do you mean with "at every 5 numbers"? – Nina Scholz Commented Jan 18, 2021 at 10:28
- @NinaScholz meaning 1 to 5, 6 to 10, 11 to 15, 16 to 20 and so on till 315 – Khushbu Vaghela Commented Jan 18, 2021 at 10:28
-
The first number maybe
1
or2
or any number. After that, the next number just be theprevious number + 5
? – user15026033 Commented Jan 18, 2021 at 10:33 - Does this answer your question? Generating random whole numbers in JavaScript in a specific range? – t3__rry Commented Jan 18, 2021 at 10:34
- 1 I'm sure this is frustrating, because it's clear in your head, but you need to take a breath and explain more clearly what you're trying to do. It would be useful to provide more than one possible sequence, so we're not misled by accidental patterns in one example. – IMSoP Commented Jan 18, 2021 at 10:42
8 Answers
Reset to default 5Here's an easy way to generate random numbers between a range of 5 (including the borders as mentioned), till the maximum number is reached.
The Running example:
function randomInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function example() {
var maximumNumber = 21;
var generatedNumbers = [];
for (var iteration = 1; iteration+1 <= maximumNumber; iteration+=5)
{
generatedNumbers.push(randomInteger(iteration, iteration+4));
}
document.getElementById("answer").innerHTML = generatedNumbers;
}
<button onclick="example()">Generate</button>
<p id="answer"></p>
You could iterate the outer range with increment by five and take a random offset of max five.
for (let i = 0, l = 315; i <= l; i += 5) {
console.log(i += Math.floor(Math.random() * 5));
}
you can try something like this (I didnt tested, but it came to my mind):
<script>
function randomIntFromInterval(min, max) { // min and max included
var i = min;
var numbers = []
for ( i; i < max; i+5)
numbers.push(Math.floor(Math.random() * (max - i+ 1) + i));
return numbers;
}
</script>
The essence of programming is breaking a problem down into smaller parts, and then building back up to the full solution.
In this case, we can break the problem down like this:
- Create a set of 63 bands: 1 to 5, 6 to 10, etc
- Pick a random number in each band
For part 2, you already have the getRndInteger
function you show in the question:
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
Part 1 can be done a few ways, but perhaps the easiest to understand is a simple while
loop:
let min=1;
let max=5;
while ( max <= 315 ) {
console.log(min, max);
min += 5;
max += 5;
}
So now all we need to do is put the two together:
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
let min=1;
let max=5;
while ( max <= 315 ) {
let next = getRndInteger(min, max);
console.log(next);
min += 5;
max += 5;
}
You have to loop to find the all random numbers in the range you want, change the max and min values in each loop. Since all numbers are requested, you must add them to an array.
<button onclick="document.getElementById('demo').innerHTML = randomIntFromInterval()">Click Me</button>
<p id="demo"></p>
function randomIntFromInterval() {
// min and max included
let min=0;
let max=5;
let numberOfList=[];
for(let i=0;i<63;i++){
let randomNumber= Math.floor(Math.random() * (max - min +1) + min);
numberOfList.push(`${randomNumber}`);
min=min+5;
max=max+5;
} return numberOfList;
}
Using rando.js for simplicity and cryptographic security:
for(var i = 1; i <= 315; i += 5){
console.log(rando(i, i + 4));
}
<script src="https://randojs./2.0.0.js"></script>
Let's say the first number may be any integer ranging from 1 to 315
.
After that, the subsequent number equals the previous number + 5
.
var firstNumber = 0;
function randomIntFromInterval(min, max) {
var randomNumber = Math.floor(Math.random() * (max - min + 1) + min);
var result = [randomNumber];
while(randomNumber + 5 <= 315){
randomNumber += 5;
result.push(randomNumber);
}
return result.join(',');
}
<button onclick="document.getElementById('demo').innerHTML = randomIntFromInterval(1,315)">Click Me</button>
<p id="demo"></p>
Old version answer
Besides, you can add more of a button named Reset
to random it again.
var firstNumber = 0;
function randomIntFromInterval(min, max) {
if(firstNumber ===0) {
firstNumber = Math.floor(Math.random() * (max - min + 1) + min);
return firstNumber;
}
firstNumber += 5;
return firstNumber >= 315 ? 315 : firstNumber;
}
function reset(){
firstNumber = 0;
document.getElementById('demo').innerHTML = '';
}
<button onclick="document.getElementById('demo').innerHTML = randomIntFromInterval(1,315)">Click Me</button>
<button id="Reset" onclick="reset()">reset</button>
<p id="demo"></p>
You can think like this. When Math.random() generates 0, get 1. And Math.random() generates 1, you get 5. So you can write this function like this:
Math.floor(1 + 4 * Math.random())
so you can generate like this.
for(let i = 0; i<315; i+=5)
{
console.log(Math.floor(i + 1 + 4 * Math.random()))
}