1

Is it possible to create a truly random number in JavaScript?

I have tried here jsfiddle

Javascript:

    // Mouse coordinates on click
var mouseX;
var mouseY;
$(document).ready(function(){

  $("#btn1").click(function(e){
    mouseX = e.clientX;
    mouseY = e.clientY;
  });

});


// Timing mouse down
var pressed;
var duration;

$(document).ready(function(){

  $("#btn1").on("mousedown", function(){
    pressed = +new Date();
  });
  $("#btn1").on("mouseup", function(){
    duration = +new Date() - pressed;
  });

});

// Time since last click
var loaded;
var onPush;
$(document).ready(function(){

  loaded = +new Date();
  $("#btn1").on("mousedown", function(){
    onPush = +new Date() - loaded;
    loaded = +new Date();
  });

});

// Extending process for pseudorandom number? or creating a truly random number?
$(document).ready(function(){
  $("#btn1").on("click", function(){

    function rnum(min, max) {
      var a = Math.random();
      var ranMouseX = mouseX * a;
      var ranMouseY = mouseY * a;
      var ranDuration = duration * a;
      var ranOnPush = onPush * a;

      var combine = ((ranMouseX / ranOnPush) + (ranMouseY / ranDuration) % a);
      $("#p1").text(((combine - Math.floor(combine)) * (max-min) + min).toFixed(0))
    };
    var f = document.getElementById('min');
    var g = document.getElementById('max');
    rnum(parseInt(f.value), parseInt(g.value));
  });
});

But have I just extended the process for a pseudo-random number? or does this make a truly random number?

If it is still just a pseudo-random number how can I make a truly random number in JavaScript?

6
  • 4
    truly random numbers require a truly random source of noise, usually physical....Trying to make your own 'more' random numbers invariably leads to generating something less random! Random number generation is best left to the experts. Commented Mar 23, 2017 at 3:40
  • Math.random() is just fine, don't really understand what you're looking for. From that original random number, just perform some calculation/computation to get your own random numbers (with such as greater values, ...). Commented Mar 23, 2017 at 3:45
  • 1
    random.org Commented Mar 23, 2017 at 3:46
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 23, 2017 at 3:47
  • 1
    Beyond the emotional satisfaction of saying "truly random" -- what are you trying to accomplish? It is a fairly deep area. JavaScript's PRNG is adequate for most purposes (though it does fall short of e.g. Python's). You could always implement your own version of e.g. the Mersenne Twister -- but do you really need it? This is interesting: v8project.blogspot.com/2015/12/… Commented Mar 23, 2017 at 3:49

2 Answers 2

3

From default APIs, you'll be stuck with pseudo-random (truly random would require a physical interface).

However, the Crypto interface does provide a crypto-safe random number generator : Crypto.getRandomValues() which seeds have more entropy than the default Math.random() method.

var array = new Uint32Array(10);
window.crypto.getRandomValues(array);

for (var i = 0; i < array.length; i++) {
    console.log(array[i]);
}

Sign up to request clarification or add additional context in comments.

1 Comment

How can I use this to hex 64 characters of Hex Values?
1

There is an API - ANU Quantum Random Number Generator API that you can use. From the description:

The numbers are considered to be "truly random", because they are generated by measuring the quantum fluctuations of a vacuum

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.