Jump to content

Random numbers


nath.bplb

Recommended Posts

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;

Link to comment
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;

Link to comment
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

Link to comment
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.
Link to comment
Share on other sites

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

Link to comment
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.... :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...