Win a copy of The Java Performance Companion this week in the Performance 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: 15359
39
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
Pie
Posts: 49405
62
  • 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