File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes About Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Array" Watch "About Array" New topic

About Array

eLL Pascual

Joined: May 22, 2008
Posts: 21
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?

abhishek pendkay
Ranch Hand

Joined: Jan 01, 2007
Posts: 184
Can I make a program which is randomly selecting an int value from an array

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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15037

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 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45289
Even easier if you find the correct method in java.util.Random.
Consider Paul's rocket mass heater.
subject: About Array
It's not a secret anymore!