By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,650 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,650 IT Pros & Developers. It's quick & easy.

Random number generator

P: 1
Random number it should be in between 0 to the my arraylist size
Mar 1 '12 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
Use java.util.Random.
Mar 1 '12 #2

P: 21
Try this....

Expand|Select|Wrap|Line Numbers
  1. import java.util.ArrayList;
  2. class RandomNumberGenerator{
  3. public static void main(String args[]){
  4. ArrayList<String> al=new ArrayList<String>();
  5. al.add("22");
  6. al.add("32");
  7. al.add("52");
  8. al.add("72");
  9. al.add("12");
  10.  
  11. int num=(int)Math.round(Math.random()*al.size());
  12. System.out.println(num);
  13. }
  14. }
  15.  
  16.  
Mar 1 '12 #3

P: 21
If you want to use java.util.Random class

then follow this

Expand|Select|Wrap|Line Numbers
  1. Random r=new Random();
  2. int num=r.nextInt(al.size());
  3.  
Mar 1 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.