I was coding to understand how html work but I could not understand the Math.random() function fully.
<!DOCTYPE html>
<html>
<body>
Value:<br> <p id="Value"> 0 </p>
<div>
</div>
<p>Click the button to increase the value.</p>
<button onclick="increase()"+",this.blur()" id="demp" style="position:relative; left: 500px; top: 80px; width: 64px; height: 64px;">Increase</button>
<script>
function increase() {
var x = document.getElementById("Value").innerHTML;
var increased = parseInt(x,10) + parseInt(1,10);
document.getElementById("Value").innerHTML = increased;
}
function change(){
var num = 0;
window.setInterval(function(){
var speedx= Math.floor((Math.random() * 10) - 5);
var speedy= Math.floor((Math.random() * 10) - 5);
document.getElementById("demp").style.left= parseInt(document.getElementById("demp").style.left, 10) + parseInt(speedx,10) +"px";
document.getElementById("demp").style.top= parseInt(document.getElementById("demp").style.top, 10) + parseInt(speedy,10) +"px";
}, 10)
}
window.onload = function start() {
change();
}
</script>
</body>
</html>
I have created a button which is changing it's position according to Math.random() value. I refresh the page like 30 times and my button always going up and left. That means math.random() function produce greater then 0.5 Can you explain why it doesn't produce real random numbers and how can I create a real random number?
Aucun commentaire:
Enregistrer un commentaire