Random number it should be in between 0 to the my arraylist size
Random number generator
Collapse
X
-
Try this....
Code:import java.util.ArrayList; class RandomNumberGenerator{ public static void main(String args[]){ ArrayList<String> al=new ArrayList<String>(); al.add("22"); al.add("32"); al.add("52"); al.add("72"); al.add("12"); int num=(int)Math.round(Math.random()*al.size()); System.out.println(num); } }
Comment
-
If you want to use java.util.Rando m class
then follow this
Code:Random r=new Random(); int num=r.nextInt(al.size());
Comment
Comment