This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Array

 
eLL Pascual
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48410
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even easier if you find the correct method in java.util.Random.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic