html - Javascript random number generator in range, going below and above the given range - Stack Overflow

I have been trying to make a random number generator, with a given range, thought it would be easy and

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>

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>
Share Improve this question asked Apr 27, 2018 at 17:56 Plamen TsanevPlamen Tsanev 492 silver badges4 bronze badges 1
  • Math.floor(Math.random() * max + min); This causes that Math.random() is multiplicated with max and after that, min is added via the plus. – Tyr Commented Apr 27, 2018 at 17:59
Add a ment  | 

4 Answers 4

Reset to default 3

You need to multiply Math.random() by the length of your range, not by the maximum value in the range.

const getRandom = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);
Array.from({ length: 20 }, () => console.log(getRandom(0, 2)));

  1. document.getElementById("max").value is string, so now Math.random() * max + min is float * string + string, which append min value as string. parseInt will resolve this issue.
  2. Math.random() * max + min is incorrect. Math.random() * (max - min + 1) + min is right.

Add a code block that checks if the random number generated falls in the range specified. If it doesn't loop through random number generation until the condition test fails;

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);

  while (random > max || random < min) {
    random = Math.floor(Math.random() * max + min);
  }

  output.value = random;

}
.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;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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">

after that, you can then assign the its value to output.

using this code:

// the initial seed
Math.seed = 6;

// in order to work 'Math.seed' must NOT be undefined,
// so in any case, you HAVE to provide a Math.seed
Math.seededRandom = function(max, min) {
    max = max || 1;
    min = min || 0;

    Math.seed = (Math.seed * 9301 + 49297) % 233280;
    return Math.floor(min + (Math.seed / 233280) * (max - min));
}
var res = Math.seededRandom(100,-100)
console.log(res)

or try it yourself

http://indiegamr./generate-repeatable-random-numbers-in-js/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745133780a4613102.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信