JavaScript Random
Math.random()
Math.random()
returns a random number between 0 (inclusive), and 1 (exclusive):
Example
// Returns a random number:
Math.random();
Math.random()
always returns a number lower than 1.
JavaScript Random Integers
Math.random()
used with Math.floor()
can be used to return random integers.
There is no such thing as JavaScript integers.
We are talking about numbers with no decimals here.
Example
// Returns a random integer from 0 to 9:
Math.floor(Math.random() * 10);
Example
// Returns a random integer from 0 to 10:
Math.floor(Math.random() * 11);
Example
// Returns a random integer from 0 to 99:
Math.floor(Math.random() * 100);
Example
// Returns a random integer from 0 to 100:
Math.floor(Math.random() * 101);
Example
// Returns a random integer from 1 to 10:
Math.floor(Math.random() * 10) + 1;
Example
// Returns a random integer from 1 to 100:
Math.floor(Math.random() * 100) + 1;
A Proper Random Function
As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes.
This JavaScript function always returns a random number between min (included) and max (excluded):
Example
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min) ) + min;
}
This JavaScript function always returns a random number between min and max (both included):
Example
function getRndInteger(min, max) {
return Math.floor(Math.random() * (max - min + 1) ) + min;
}
33 Responses
brand levaquin 500mg levofloxacin without prescription
avodart 0.5mg pills dutasteride online buy buy ondansetron 8mg without prescription
spironolactone 100mg uk order finasteride generic brand diflucan
buy acillin online acillin over the counter oral erythromycin
sildenafil cheap order generic fildena 50mg methocarbamol 500mg cost
buy tadalis 10mg online order generic tadalafil 20mg order diclofenac 100mg online cheap
buy isotretinoin for sale order amoxil 500mg zithromax price
indocin 75mg pill suprax 100mg canada buy amoxicillin pills
cialis pharmacy buy no rx cialis cheap viagra online
buy anastrozole 1 mg pills Us discount viagra overnight delivery viagra 100mg price
tadalafil 40mg sans ordonnance en france viagra 200mg sans ordonnance en pharmacie viagra generique en pharmacie
prednisone sale mail order cialis sildenafil online
tadalafil 5mg für männer cialis ohne rezept sildenafil 200mg kaufen
isotretinoin 40mg without prescription order stromectol 3mg generic ivermectin brand name
order provigil 200mg pills Cialis in usa order diamox generic
doxycycline order vardenafil 10mg without prescription furosemide 100mg price
cheap catapres order tiotropium bromide 9mcg order tiotropium bromide 9 mcg without prescription
order buspirone 10mg for sale oral ditropan oxybutynin 5mg tablet
hytrin drug azulfidine 500mg tablet order sulfasalazine 500mg online cheap
buy olmesartan online cheap diamox pills diamox 250mg for sale
buy isosorbide online cheap buy imuran generic order telmisartan 20mg online
molnunat pills naproxen 250mg sale lansoprazole 30mg price
purchase salbutamol online cheap buy sildenafil 100mg generic buy generic viagra 50mg
cialis without a doctor prescription prozac 40mg canada order sildenafil 100mg pills
buy tadalafil 5mg without prescription cialis 20mg cost symmetrel 100mg for sale
dapsone 100mg pill buy allegra 120mg online buy perindopril for sale
cheap provera 5mg brand microzide 25mg cyproheptadine 4mg cheap
purchase modafinil for sale stromectol oral ivermectin 6 mg for sale
buy accutane 10mg online cheap amoxicillin 500mg tablet buy deltasone 5mg online cheap
azithromycin canada purchase prednisolone online order neurontin for sale
tadalafil brand order tadalafil 40mg order viagra 50mg generic
buy furosemide 40mg pills hydroxychloroquine 200mg usa order plaquenil 400mg pill
cheap tadalafil pill order clomipramine 50mg pill purchase anafranil online