How to generate cryptographically random numbers?

How to generate random rational numbers between 1.0 and 0.0 in specific range (with JavaScript)?

  • I'm trying to generate better random numbers in JavaScript with min/max value, since this is a web game, the random function might be in use hundreds of times (or more) a second, i have noticed that using Math.random() won't give the best results. So i found some nice algorithms implemented with js: http://baagoe.org/en/w/index.php/Better_random_numbers_for_javascript. Using an implementation-dependent algorithm or strategy" means that you don't know how the "random" numbers are made and how "random" they are likely to be, ... This here's how i generate my random numbers now with one of them: var rand1 = Alea().uint32(); var rand2 = Alea(rand1); rand1 is a random integer, it's used as a seed for rand2, a random number between 1 and 0. I'm looking for a way to limit it's range to let's say 0.3 and 0.6 or 0.003 and 0.006, by doing as little as possible processing while keeping high "randomality". here's how i limit the range of integers: Math.floor(rand2 * (max - min + 1)) + min

  • Answer:

    var rand3 = 0.3*rand2+0.3 Will give you random number between 0.3 and 0.6, assuming rand2 is random between 0. and 1. Generally that is what you do in such situations, shift and rescale your number, these operations preserve "randomness"

Jerzy Michał Pawlak at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.