Muteking Muteking - 1 year ago 169
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 Source

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">

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download