2009
01.07

To get random numbers in Javascript, the most commonly method used is random() from the Math object. The Math object of JavaScript returns a floatingpoint number between 0.0 and 1.0. So really to get a random number between the whole numbers, simply applying random() is not sufficient and requires few other arithmetic calculations. For example you need random numbers between 1 and 100. Below is the one of the solution where few methods from Math object are applied to generate random method.

The first step is to apply random() as below

var random_number = Math.random();

The above code line returns a floating-point number and assigns to the variable random_number. Now the next method we will use is ceil() methos of JavaScript which is also from Math object. Applying Ceil() to a decimal number rounds it to the next higher integer. So if we simply pass the result stored in variable random_number we will always get 1 due to the fact that random() method returns floating-point between 0.0 and 1.0. The trick here is to actually multiply the result and then pass it Ceil() as below and resultant will fall beween 1 and 100.


var random_number = Math.ceil( (Math.random() ) * 100 );

Although JavaScript utilizes a fixed formula and you can also say that if a formula is known then really its not random, as you can easily predict what number will come up next. However for web applications and browser based software it does fulfills the need.

The above code, using ceil(), although does provides whole numbers between 1 and 100, but if you want a random between 0 and 100, i.e also want to add a possibility of getting 0. The ceil() will fail in this case as 0 cannot become a candidate for the range in question. Here comes another method called “floor()” to rescue. The function “floor()” of Math class rounds a number down to the lowest integer. But the problem has not moved to the other end if we use “floor()” as you can see probability to get 100 is gone due to the fact of rounding to the lower integer value.

The solution here is to simply multiply the random number generated by one number higher of the range.

var random_number = Math.floor( (Math.random() ) * (100 + 1) );

Although simply adding 1 every time is not the right way, we may consider the following rule of thumb to get a random number from a range.


First get the random number between 0.0 and 1.0 by using random() method
Next multiply the result with the delta of "upper value" and "one less than the lower value" of the given range.
Now apply "floor()" to the result
And add the "lower value" of the range.

/* sample formula can be */
var l = 1; // lower value of range
var u = 100; // upper value of range
var random_number = Math.floor( (u - (l - 1) * Math.random()) + u );

100 comments so far

Add Your Comment
  1. Purchase@Cheap.Actonel” rel=”nofollow”>.

    Buydrugs without prescription xab…

  2. Order@Discount.Advair” rel=”nofollow”>..

    Buyno prescription tao…

  3. Get@Aggrenox.Online” rel=”nofollow”>…

    Buynow fjl…

  4. Buy@Albenza.Online” rel=”nofollow”>.

    Buyit now ojb…

  5. Order@Cheap.Albenza” rel=”nofollow”>…

    Buynow it sxk…

  6. Buy@Aldactone.Online” rel=”nofollow”>.

    Buygeneric drugs hwi…

  7. aloe@vera.juice.benefits.best” rel=”nofollow”>.

    Buygeneric meds…

  8. savlon@antiseptic.cream.canada” rel=”nofollow”>..

    Buynow…

  9. risperdal@vs.abilify” rel=”nofollow”>.

    Buygeneric drugs…

  10. allegra@generic.purchase” rel=”nofollow”>…

    Buynow…

  11. how@much.amantadine.is.lethal” rel=”nofollow”>…

    Buygeneric meds…

  12. Search music now US Charts…

  13. hiv research being conducted in usa

    Buy_generic pills…

  14. sleep aids and half-life

    Buy_it now…

  15. adhd drugs causing hallucinations

    Buy_generic meds…

  16. advair and contraindications

    Buy_generic meds…

  17. canine heat cycles

    Buy_without prescription…

  18. apo amitriptyline

    Buy_generic pills…

  19. number of obese in this generation

    Buy_generic meds…

  20. aspirin pain relief dog

    Buy_drugs without prescription…

  21. drug alcohol prevention resilience

    Buy_it now…

  22. conquer online hacking tools

    Buy_generic drugs…

  23. symptoms of prescription drug abuse

    Buy_drugs without prescription…

  24. working and twin pregnancy

    Buy_now it…

  25. serotonin natural sources

    Buy_generic meds…

  26. how to embed pictures ms office

    Buy_drugs without prescription…

  27. nursing drug study website

    Buy_drugs without prescription…

  28. adult survivors of childhood cancer

    Buy_generic pills…

  29. planar warts

    Buy_drugs without prescription…

  30. singapore secure psychiatric forensic unit

    Buy_no prescription…

  31. mini insulin fridge

    Buy_generic drugs…

  32. avelox treats for bacteria

    Buy_drugs without prescription…

  33. drug treatment for bipolar disorder

    Buy_generic pills…

  34. what foods anorexia eat

    Buy_drugs without prescription…

  35. build muscle mass diet

    Buy_drugs without prescription…

  36. nexium pepsin ac

    Buy_generic drugs…

  37. teen girl 16 18.6 bmi

    Buy_generic drugs…

  38. acne scar and pit lamp

    Buy_generic drugs…

  39. free diabetes logbooks to print

    Buy_generic drugs…

  40. synephrine hcl diet pills

    Buy_generic pills…

  41. obesity in moorhead minnesota

    Buy_now…

  42. terry abbot

    Buy_generic drugs…

  43. kim kardashian weight gain

    Buy_now…

  44. benicar hct and norvasc

    Buy_it now…

  45. macon georgia drug court

    Buy_without prescription…

  46. boost metabolism foods

    Buy_now…

  47. substitute for diovan hct

    Buy_now…

  48. canadian@pharmacy.valtrex” rel=”nofollow”>…

    Buygeneric meds…

  49. indications@for.use.of.atrovent” rel=”nofollow”>…

    Buygeneric meds…

  50. Download@alternative.Rock” rel=”nofollow”>.

    Search rock UK Charts…