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
Solved

initialise var

Posted on 2004-04-19
8
191 Views
Last Modified: 2010-03-31
How can i initialise an integer variable to be 1-10?
0
Comment
Question by:jtcy
8 Comments
 
LVL 1

Expert Comment

by:b612_forever
ID: 10865603
java.util.Random rand = new java.util.Random();
int i = rand.nextInt(11);

// the 11 means (10 + 1) beacuse the parameter 11 is exclusive.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10865629
No, that delivers 0 - 10, correct is:

    int i = rand.nextInt(10) + 1;


;JOOP!
0
 

Author Comment

by:jtcy
ID: 10865666
would this work?

int ballToGoal = (int) (Math.random () * 10) ;
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:sciuriware
ID: 10865684
No jtcy, that produces 0 - 9

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10866063
>> int ballToGoal = (int) (Math.random () * 10) ;

int ballToGoal = ( int ) Math.round ( ( Math.random () * 10 ) ) ; could work. Or: int ballToGoal = ( int ) ( Math.random () * 11 ) ;

- because random () returns double values greater than or equal to 0.0 and lesser than 1.0. So when you take the ( int ) part of Math.random () * 10, it will always lesser than or equal to 9 (even 9.9 would be converted to 9, that way).
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 20 total points
ID: 10866686
>>
would this work?

int ballToGoal = (int) (Math.random () * 10) ;
>>

Almost, but it would produce 0 - 9. You need



int ballToGoal = 1 + (int) (Math.random () * 10) ;
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10871376
Like I said.

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10871403
Yes - missed that. But it's better if he has the right code
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MySqlDump not dumping triggers 1 43
Java string replace 11 53
ejb message driven bean mdb creation steps 2 15
learn programming 8 29
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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 …

792 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