There is such a code

function randomInteger(min, max) { var rand = min - 0.5 + Math.random() * (max - min + 1) rand = Math.round(rand); return rand; } alert( randomInteger(5, 10) ); 

It is necessary to tune it for the output in the desired span on the class. From 1000 to 9999 (a four-digit number should be). Prompt the principle of how to implement.

  • four
    Prompt the principle of how to implement. The function has two parameters. You have 2 numbers in conditions. It remains not to confuse ... - Akina

3 answers 3

The output in the span can be implemented through getElementById or getElementsByClassName , but when searching by class, you need to specify the index of this element, since there may be several, it is better to use the search by id .

 function randomInteger(min, max) { var rand = min - 0.5 + Math.random() * (max - min + 1) rand = Math.round(rand); return rand; } document.getElementById('span-id').innerHTML = randomInteger(1000, 9999); document.getElementsByClassName('span-class')[0].innerHTML = randomInteger(1000, 9999); 
 <span id="span-id"></span> <br/> <span class="span-class"></span> 

  • Understood) thanks, it works, I’m just breaking into JS, so I don’t know how to do these simple tasks yet :) - Rafael Shepard
 $('span.class').text(randomInteger(5,10)); 

    This is how any numbers can be + mini-validation.

     function randomInteger(min, max) { var rand = min - 0.5 + Math.random() * (max - min + 1) rand = Math.round(rand); document.getElementsByClassName('random')[0].innerHTML = `Случайное число ${rand}`; } document.getElementsByClassName('startRandom')[0].addEventListener('click', ()=> { let min = document.getElementsByClassName('min')[0].value; let max = document.getElementsByClassName('max')[0].value; if(min < max) { randomInteger(min, max); } else { document.getElementsByClassName('random')[0].innerHTML = 'Ведите валидные числа' } }); 
     .random, div { padding: 5px; } 
     <div class='random'></div> <div>min: <input type="number" class='min'></div> <div>max: <input type="number" class='max'></div> <button class='startRandom'>RANDOM!</button> 

    • Very cool option)! Thank you - Rafael Shepard
    • The man on top sent first, sorry) - Rafael Shepard