Solved

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

Posted on 2001-09-16
4,468 Views
from Right

How can I generate a random integer from 1 to 10 using the Math.random() method.
0
Question by:Right
• 3
• 2

LVL 6

Expert Comment

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

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

Lab_Rat earned 100 total points
this looks a cool method!
0

LVL 92

Expert Comment

Interesting answer to accept.
Remind me not to answer any of your questions again.
0

LVL 1

Expert Comment

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

Lab Rat,

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

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Title # Comments Views Activity
Logs to delete in Windows VCenter server ? 11 156
json example 39 110
word0 challenge 4 52
java set up 1 44
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

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

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

#### Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!