Solved

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

Posted on 2001-09-16
6
4,468 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
Comment Utility


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
Comment Utility
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
Comment Utility
this looks a cool method!
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 92

Expert Comment

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

Expert Comment

by:Lab_Rat
Comment Utility
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
Comment Utility
Lab Rat,

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now