Solved

using Math.random to generate an integer from 1 to 10

Posted on 2001-09-16
6
4,487 Views
Last Modified: 2011-09-20
from Right

How can I generate a random integer from 1 to 10 using the Math.random() method.
0
Comment
Question by:Right
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 6486365


int intNum = (int) 10 * java.lang.Math.random();

Since Random returns a a double between 0.0 and 1.0, multipling by 10 will return a value between 0.0 and 1.0. Then cast it to an int.
0
 
LVL 92

Expert Comment

by:objects
ID: 6486416
You better off using the nextInt() method in the Random class. And if you're using a JDK prior to 1.2 then here's the code:

public int nextInt(int n)
{
     if (n<=0)
          throw new IllegalArgumentException("n must be positive");

     if ((n & -n) == n)  // i.e., n is a power of 2
         return (int)((n * (long)next(31)) >> 31);

     int bits, val;
     do {
         bits = next(31);
         val = bits % n;
     } while(bits - val + (n-1) < 0);
     return val;
 }
 

Using Math.random() doesn't allow you any control over the random number seeding and will also be slower due to unnecessary floating point arithmetic.
0
 
LVL 1

Accepted Solution

by:
Lab_Rat earned 100 total points
ID: 6490826
this looks a cool method!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 6492001
Interesting answer to accept.
Remind me not to answer any of your questions again.
0
 
LVL 1

Expert Comment

by:Lab_Rat
ID: 6495335
LOL!

Objects, pitty this isn't rated a hard question! :o(

ahem!

Anyhow, I don't deserve the points (and a 'B' rating for such an amazing answer as mine? bah!), so I'f you want to get the points you deserve from the mods, I wont take offence. (I think Right was just asleep...)

:o)
0
 
LVL 92

Expert Comment

by:objects
ID: 6495404
Lab Rat,

It's not worth worrying about.
Thanks for the concern :)
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question