Muteking Muteking - 1 month ago 5
Javascript Question

Javascript random number between 0 and 1

I can't seem to generate a random float number from 0.0 to 1.0 with

Math.floor((Math.random() * 1.0) + 0.0);


Is there a workaround for that?

Answer

Math.random() as is will generate a random float number between 0.0 and 1.0.

Working example:

document.getElementById("generate").onclick = function() {
  document.getElementById("random").value = Math.random();
};
<button id="generate">Generate random number</button>
<input id="random">

Comments