-1

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. 1,6,14,20,23,27,31,39...
  2. 3,8,15,16,22,29,32,37...
  3. 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?

Khushbu Vaghela
  • 609
  • 2
  • 5
  • 18
  • what do you mean with *"at every 5 numbers"*? – Nina Scholz Jan 18 '21 at 10:28
  • @NinaScholz meaning 1 to 5, 6 to 10, 11 to 15, 16 to 20 and so on till 315 – Khushbu Vaghela Jan 18 '21 at 10:28
  • The first number maybe `1` or `2` or any number. After that, the next number just be the `previous number + 5`? –  Jan 18 '21 at 10:33
  • Does this answer your question? [Generating random whole numbers in JavaScript in a specific range?](https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range) – t3__rry Jan 18 '21 at 10:34
  • @StackOverFlow No it not always should be previous+5. It should be random number. – Khushbu Vaghela Jan 18 '21 at 10:34
  • For example: the first number is `2`, then the next number maybe `7` or `11` till 315, right? –  Jan 18 '21 at 10:35
  • @t3__rry No I already check that out but it is not what I want. – Khushbu Vaghela Jan 18 '21 at 10:39
  • @StackOverFlow NO. If first number is 2 (from 1 to 5), second number should be 7 (from 6 to 10). and not 11. 11 will be 3rd number. – Khushbu Vaghela Jan 18 '21 at 10:40
  • 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 Jan 18 '21 at 10:42
  • @IMSoP yes I understand. Please check my edits in question for more clearer view. – Khushbu Vaghela Jan 18 '21 at 10:48

8 Answers8

5

Here'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>
Deepak
  • 2,660
  • 2
  • 8
  • 23
  • 1
    Why are you excluding 1 and 5? I don't see that mentioned in the question anywhere. – IMSoP Jan 18 '21 at 11:16
  • 1
    Yeah thanks, edited it. Maybe the examples made me think of it :P Now I see the OP has added examples including border digits ;) – Deepak Jan 18 '21 at 11:23
  • 1
    Thanks this is what I want @Deepak – Khushbu Vaghela Jan 18 '21 at 11:31
  • @Deepak If I need to add string of 315 in an array. Should it be done with strings too? like var generatedNumbers = ["1 abc", "2 qwe", "3 cbv", "4 fdd"]; ? – Khushbu Vaghela Jan 18 '21 at 12:02
  • @Khushbu Vaghela Oh no, why I've received `5,6,14,17` if I click the generate button multiple times. Obviously, It's not stable. Having bug @@ – Nguyễn Văn Phong Jan 19 '21 at 02:03
  • 1
    @Phong That's the expected result. Hope you read all the examples provided by the OP :) – Deepak Jan 19 '21 at 02:49
  • 1
    @KhushbuVaghela Do you need to append strings with the generated number? If so, just store the generated number in a variable, and append the string to it before pushing it to the array – Deepak Jan 19 '21 at 02:52
  • @Deepak I tried it like: var temp = generatedNumbers ; and before pushing it to the array temp += "abc"; But "abc" shows at the end of the array like: "1,6,14,19,22,30....313 abc" I want like "1 abc, 2 asx, 3 ads..." – Khushbu Vaghela Jan 19 '21 at 06:17
  • 1
    "generatedNumbers" is an array. You are appending the string to that array. That's why you get that result. Just keep this inside your for loop: var generatedNumberWithString = randomInteger(iteration, iteration+4) + "abc"; generatedNumbers.push(generatedNumberWithString); – Deepak Jan 19 '21 at 06:22
  • @Deepak Tahnk you it is working but I want different string on different numbers. not only "abc". Is it possible? – Khushbu Vaghela Jan 19 '21 at 06:32
  • 1 abc,2 xyz,3 pqr and like this on each number – Khushbu Vaghela Jan 19 '21 at 06:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227517/discussion-between-khushbu-vaghela-and-deepak). – Khushbu Vaghela Jan 19 '21 at 07:54
2

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));
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 3
    Oh no, the result may be `3 10 17` ?? It doesn't be `+5` each time :( –  Jan 18 '21 at 11:00
  • @StackOverFlow, it doesn't have to. please see the example of op. – Nina Scholz Jan 18 '21 at 11:03
  • 1
    I just ran this a couple of times, and one of them included the sequence "22,27,36,42", which I don't think meets the OP's requirements - note the jump of 9 in the middle there. – IMSoP Jan 18 '21 at 11:14
  • 4
    Oh, I see the bug now: you're adding a fixed offset of 5 _and_ a random offset to the same variable, on every loop. So the number goes up by between 5 and 10 each time, which could never generate the example sequence 2,7,11 (offset of 4 between the second and third numbers). – IMSoP Jan 18 '21 at 11:33
1

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>
Max_E
  • 9
  • 3
1

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>
  • The question has been edited to clarify that the sequence is _not_ just adding 5 each time. – IMSoP Jan 18 '21 at 11:12
  • @StackOverFlow Thank you but I want series of numbers. In one click all the series should be shown. – Khushbu Vaghela Jan 18 '21 at 11:22
  • 1
    @KhushbuVaghela As I mentioned in my answer, always break your problems down: whether the sequence is output one item at a time, pushed into an array, built into a string with commas; and whether it's triggered by a click, or when the page loads, or when the clock strikes 12 ... all those are details you can change for _any_ of the suggestions on this page. Get the algorithm right first, and then you can reuse it however you like. – IMSoP Jan 18 '21 at 11:28
  • @IMSoP yes right but still the series does not start from 1 to 5 ! and it always adds 5 which I do not want – Khushbu Vaghela Jan 18 '21 at 11:35
  • @KhushbuVaghela Sure, I wasn't saying this was necessarily the right algorithm, I just wanted to stress the importance of looking at _algorithms_ as separate from _how they're used_. – IMSoP Jan 18 '21 at 11:37
  • @IMsoP I've just updated my answer based on your new requirement. Pls take a look at it, Thanks. –  Jan 19 '21 at 01:58
  • @Khushbu Vaghela You should take a look at this answer one again. – Nguyễn Văn Phong Jan 19 '21 at 02:03
  • @StackOverFlow You've missed the more important change, which is adding 5 each time isn't the sequence the OP wanted. See the updated question, and the accepted answer. – IMSoP Jan 19 '21 at 08:59
1

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:

  1. Create a set of 63 bands: 1 to 5, 6 to 10, etc
  2. 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;
}
IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Why does it start from 65 to 70 ? – Khushbu Vaghela Jan 18 '21 at 11:37
  • I'm not sure. It seems to be something wrong with the code snippet widget, I think - maybe it just has a maximum number of lines it can show? If I paste it into an actual browser console, all the numbers are there, and hopefully the code should be clear enough to see that it does start at 1. – IMSoP Jan 18 '21 at 11:38
1

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;
}
1

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.com/2.0.0.js"></script>
Aaron Plocharczyk
  • 2,776
  • 2
  • 7
  • 15
0

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()))
}
Sato Takeru
  • 1,669
  • 4
  • 12
  • 27