Solved

initialise var

Posted on 2004-04-19
8
188 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
 
LVL 24

Expert Comment

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

;JOOP!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

by:mayankeagle
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

21 Experts available now in Live!

Get 1:1 Help Now