473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Random number generator

Random number it should be in between 0 to the my arraylist size
Mar 1 '12 #1
3 2170
r035198x
13,262 8TB
Use java.util.Random.
Mar 1 '12 #2
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
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

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

Similar topics

22
by: Alan Lee | last post by:
Is there any way to set RAND_MAX in rand()? I am looking for a way to get a random number between 1 and 1E9. So far I can't figure out how to do that. any suggestions would be greatly appreciated.
1
by: Joseph | last post by:
Hi all, I am a newbie with a problem,I want to have a random number generator which generate numbers satisfied normal distribution(gaussian).I found that GNU GSL lib provids some functions...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
9
by: L33VaNcL33F | last post by:
I like to make a javascript that generate random number from the range number within : (10016486 and 99999985). the number always + 22423 that begin from 10016486 and end at 99999985 Example...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
4
by: Lexicon | last post by:
Hello; Does anyone know a 64-bit integer generator for 32-bit machines that use long long or __int64 etc ?
3
AccessQuestion
by: AccessQuestion | last post by:
So just a quick question; more out of curiosity than anything. I have a form in which I pop a random number into a text box. Eventually that number will be placed by a user using a scale, but I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.