# Random numbers

## Recommended Posts

Can anyone tell me how to display random numbers between 20 to 50...

##### Share on other sites

OK, all you need to do is display random numbers between 0 and 30 and then add 20 to it.

`rnd = Math.floor(Math.random() * 30) + 20;`

##### Share on other sites

Thanks, You are a genious man !!!

##### Share on other sites

They're things you just have to learn. Or learn how to deduce, if you're into mental training.

##### Share on other sites

I'm learning web designing here since a long years,BTW i think the code should be

`rnd=Math.floor(Math.random()*31)+20;`

##### Share on other sites

Math.random() choose a random number between 0-1 lets think that it will choose 0.999999 (too much close to 1)so if you make it 31 times it will be 30.999999 (to much close to 31) so far the result will be a number between 0-31 and if you add 20 to it the result will be a number between 0-51 and you don't want it

Edited by Dr.RedEyedFrog
##### Share on other sites
Math.random() choose a random number between 0-1 lets think that it will choose 0.999999 (too much close to 1)so if you make it 31 times it will be 30.999999 (to much close to 31) so far the result will be a number between 0-31 and if you add 20 to it the result will be a number between 0-51 and you don't want it
We're using Math.floor() to make sure that is always rounds down to the bottom result. The floor of 30.999999 is 30.
##### Share on other sites

ouch sorry!I didn't see Math.floor there

##### Share on other sites
Can anyone tell me how to display random numbers between 20 to 50...
hi there,iimport java.util.Random;public final class RandomRange { public static final void main(String... aArgs){ log("Generating random integers in the range 20..50."); int START = 20; int END = 50; Random random = new Random(); for (int idx = 1; idx <= 10; ++idx){ showRandomInteger(START, END, random); } log("Done."); } private static void showRandomInteger(int aStart, int aEnd, Random aRandom){ if ( aStart > aEnd ) { throw new IllegalArgumentException("Start cannot exceed End."); } long range = (long)aEnd - (long)aStart + 1; long fraction = (long)(range * aRandom.nextDouble()); int randomNumber = (int)(fraction + aStart); log("Generated : " + randomNumber); } private static void log(String aMessage){ System.out.println(aMessage); }} try this code it will help you.... Edited by Chetan Singh
##### Share on other sites

This is a Javascript forum, not a Java forum.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.