How to create a number generator from a Home number and a Final number?

Asked

Viewed 1,714 times

4

I don’t know if it does in php or javascript, I’ve read tutorials but I couldn’t get anywhere... I would like to create a system so that I put an initial number and an end and in this range would have 20,000 numbers and were generated randomly!

example:

85000 initial number

85659
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>

  • Post what you tried to do, which helps. About being PHP or JS, it just depends on the purpose. If it is something that cannot be changed by the user, better on the PHP side.

2 answers

5

JS solution

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

PHP solution

rand( int $min , int $max )


See demo in JS:

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

4


You can do as follows below using this function randomNumber javascript:

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

alert(randomNumber(85659, 85325));

Solution based on your Javascript HTML:

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>';   
}
  • In the case here, how would I print all 20,000 on another page ? or could it be at the very bottom !

  • I changed the example, but with 20 numbers generated.

  • but how would I generate the 20,000 ?

  • Just change it to 20,000, where it’s commented.

  • 20,000 trava kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk, you could help me just this once more, as you would generate one by one or 20 by 20, on the same page, like going down....

  • If you do the second way with PHP should take but generate.

  • 20k of clicks is also great, there is no way to add a timer to generate from this code ?

  • now was, thank you !

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.