1. 程式人生 > >javascript實現隨機數生成器

javascript實現隨機數生成器

<body>

Min:<input type="text" placeholder="最小值" class="min">

Max:<input type="text" placeholder="最大值" class="max">

<button onclick="btn()">生成</button>

<input type="text" placeholder="隨機數" class="result">

</body>

<script>

function btn() {

var min = document.querySelector('.min');

var minValue = min.value;

var max = document.querySelector('.max');

var maxValue = max.value;

var result = document.querySelector('.result');

 

var m = Number(minValue) + Math.round(Math.random() * (maxValue - minValue));

 

result.value = m;

}

 

</script>