aspose file tools*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About Array" Watch "About Array" New topic
Author

About Array

eLL Pascual
Greenhorn

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?

Thanks!
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

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

Joined: Aug 16, 2005
Posts: 14347
    
  22

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
Sheriff

Joined: Oct 13, 2005
Posts: 39834
    
  28
Even easier if you find the correct method in java.util.Random.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Array