jeudi 28 décembre 2017

How can I make the unique random numbers from this code appear on my page?

Someone showed me the following code which generates 3 random numbers between 1 and 10:

var limit = 10,
    amount = 3,
    lower_bound = 1,
    upper_bound = 10,
    unique_random_numbers = [];

if (amount > limit) limit = amount; //Infinite loop if you want more unique
                                    //Natural numbers than exist in a
                                    // given range
while (unique_random_numbers.length < limit) {
    var random_number = Math.floor(Math.random()*(upper_bound - lower_bound) + lower_bound);
    if (unique_random_numbers.indexOf(random_number) == -1) { 
        // Yay! new random number
        unique_random_numbers.push( random_number );
    }
}
// 

How could I make these numbers appear in place of elements with a corresponding class? The code below is clearly wrong, but hopefully it illustrates what I'm trying to achieve:

<script type='text/javascript'>
    var random_number1 = random_number1(); 
    $('.random_number1').html(random_number1);

    var random_number2 = random_number2(); 
    $('.random_number2').html(random_number2);
</script>

<span class="random_number1"></span>  <span class = "random_number2"></span>




Aucun commentaire:

Enregistrer un commentaire