Jump to content

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

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

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

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...