vendredi 27 avril 2018

Javascript random number generator in range, going below and above the given range

I have been trying to make a random number generator, with a given range, thought it would be easy and then without any logical reason the random number isnt in any way affected by the given maximum and minimum range. Please help. Here is the code:

<!DOCTYPE html>
          <html>
           <head>
             <style>
                .elements {
                text-align: center;
                 }

           .random {
                margin-top: 100px;
                width: 275px;
                height: 200px;
                font-size: 50px;
                text-align: center;
            }

            .range {
                margin: 35px 25px;
                width: 100px;
                height: 100px;
                text-align: center;
                font-size: 30px;
            }

            .generate {
                margin-top: 50px;
                width: 250px;
                height: 35px;
                font-size: 20px;
            }
        </style>

        <script language="javascript" type="text/javascript">
            function rand()
            {
                var max = document.getElementById("max").value;
                var min = document.getElementById("min").value;

                var output = document.getElementById("output");

                var random = Math.floor(Math.random() * max + min);

                output.value = random;
            }
        </script>

    </head>
    <body>
        <div class="elements">

        <input type="text" class="random" id="output">
        <br>
        <input type="button" class="generate" value="Generate random number" onclick="rand();">
        <br>
        <h1>Maximum Number</h1>
        <input type="text" class="range" id="max">
        <h1>Minimal Number</h1>
        <input type="text" class="range" id="min">

        </div>
    </body>
</html>




Aucun commentaire:

Enregistrer un commentaire