A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
About Array
eLL Pascual
Greenhorn
Joined: May 22, 2008
Posts: 21
posted
Jun 27, 2008 00:13:00
0
Can I make a program which is randomly selecting an int value from an array?For example this is my array:
int []numArray = {12,45,36};
Can I make the class Random choose from that array? Or is there anything that can do that?
Thanks!
abhishek pendkay
Ranch Hand
Joined: Jan 01, 2007
Posts: 184
posted
Jun 27, 2008 00:35:00
0
Can I make a program which is randomly selecting an int value from an array
Yes
give it a try its very easy to write such a class
Hint :- You can use Math.random();
The significant problems we face cannot be solved by the same level of thinking which created them –
Einstein
SCJP 1.5, SCWCD, SCBCD in the making
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
3
I like...
posted
Jun 27, 2008 01:20:00
0
It's very easy. Generate a random number between 0 and the size of the array minus one. Use that random number as an index into the array.
Java Beginners FAQ
-
JavaRanch SCJP FAQ
-
The Java Tutorial
-
Java SE 7 API documentation
Scala Notes
- My blog about Scala
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
Jun 27, 2008 02:33:00
0
Even easier if you find the correct method in
java.util.Random
.
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: About Array
Similar Threads
Insert Array in Array
Thread Safe primitive and an Object Array
How to make array elements final
max value of an array
Array of Objects
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter