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
Related Q & A:
- how to generate random isotropic vectors using R?Best solution by Stack Overflow
- How to generate random number each time?Best solution by Stack Overflow
- How do I change back to profile 1.0?Best solution by Yahoo! Answers
- How do you change your profile from 2.0 to 1.0?Best solution by Yahoo! Answers
- How do you change layout 1.0 to 2.0?Best solution by freecodesource.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.