How to create a number generator from an Initial number and a Final number?

4

I do not know if it is done in php or javascript, I have read tutorials but I could not get anywhere ... I would like to create a system for me to put an initial number and an end and in that interval would have 20,000 numbers and that were generated random!

example:

  

85000 starting number

85659 Share on social network 85325
...

  

95000 final number

<form class="form-inline">
  <div class="form-group">
    <label for="inicial">Inicial</label>
    <input type="number" class="form-control" id="inicial" placeholder="Número Inicial">
  </div>
  <div class="form-group">
    <label for="final">Final</label>
    <input type="number" class="form-control" id="final" placeholder="Número Final">
  </div>
  <button type="submit" class="btn btn-default">Iniciar</button>
</form>
    
asked by anonymous 16.07.2016 / 19:52

2 answers

4

You can do this below using this randomNumber function in JavaScript:

function randomNumber(min, max) {
    return Math.floor(((max + 1) - min) * Math.random() + min);
}

alert(randomNumber(85659, 85325));

Solution based on your HTML in JavaScript:

function randomNumber(min, max) {
	return Math.floor( ((max + 1) - min) * Math.random() + min);
}

function gerarNumeros() {

  var txtInicial = document.getElementById('inicial'),
  	txtFinal = document.getElementById('final'),
    result = document.getElementById('result');
    
  var numeroInicial = parseInt(txtInicial.value),
  	numeroFinal = parseInt(txtFinal.value);
  
  var count = 0;
  
  var intervalo = setInterval(function() {

    //Gerando 20 números por vez. 
    for (var i = 0; i < 20/*20000*/; i++) {
      
      var numeroGerado = randomNumber(numeroInicial, numeroFinal);
    result.innerHTML = result.innerHTML + '<p>' + count + ' : ' + numeroGerado + '</p>';
      
      count++;
      
  }
    
    if (count === 20000) {//Quando o total de números gerados for 20000, parar de gerar.
     clearInterval(intervalo);
    }

  }, 1000);//Gerar em 1 e 1 segundo.

}
p {
  padding: 2px;
  margin: 2px;
}
<form class="form-inline">
  <div class="form-group">
    <label for="inicial">Inicial</label>
    <input type="number" class="form-control" id="inicial" placeholder="Número Inicial">
  </div>
  <div class="form-group">
    <label for="final">Final</label>
    <input type="number" class="form-control" id="final" placeholder="Número Final">
  </div>
  <button type="button" class="btn btn-default" onclick="gerarNumeros()">Iniciar</button>
</form>

<div id="result"></div>

Solution based on your HTML in PHP:

<?php

$numeroInicial = 85325;//$_GET['inicial'];
$numeroFinal = 85659;//$_GET['final'];

echo '<h1>Resultado:</h1>';

for ($i = 0; $i < 20000; $i++) {
    echo '<p>' . $i . ' : ' . mt_rand($numeroInicial, $numeroFinal) . '</p>';   
}
    
16.07.2016 / 20:18
5

JS Solution

return Math.random() * (max - min) + min;

PHP solution

rand( int $min , int $max )


See the demo at JS:

<button onclick="document.body.innerHTML += (Math.floor(Math.random() * ( 95000 - 85000 ) + 85000 ) + '<br>' );">Gerar</button><br>
    
16.07.2016 / 20:19