• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[JAVAscript]Math.random()?

Elite Diviner
Joined
Jul 15, 2005
Messages
404
Reaction score
2
How effective is this fomula in getting
1(inclusive) to 100(inclusive) value at random?

Math.floor((Math.random()+0.01) * 100);

Any suggestion for better one?


Say, my math.random() gets 0.9999
I +0.01 it, and it gives 1.0999
then times 100, i get 100.99, after I math.floor it, it is 100 right? not 110.
 
Last edited:
Ginger by design.
Loyal Member
Joined
Feb 15, 2007
Messages
2,340
Reaction score
653
Code:
function nrand(min,max){
  return Math.round(Math.random()*(max-min))+min;
}

Enjoy. This isn't uniform for values min and max, though.

This is:

Code:
function unrand(min,max){
  var n = min-1;
  while(n != min-1 && n != max+1) n = nrand(min-1,max+1);
  return n;
}
 
Last edited:
Elite Diviner
Joined
Jul 15, 2005
Messages
404
Reaction score
2
Sorry for coming back late. will test this out now
 
Back
Top