Solved

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

Posted on 2001-09-16
6
4,470 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
network + 7 81
topping2 challenge 13 83
Free Alternative to JIRA 4 91
printing a file in reverse order is easy in recursion rather than in iteration 3 51
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

862 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now