?
Solved

Dealing with random numbers and arrays in Java

Posted on 2003-03-25
5
Medium Priority
?
183 Views
Last Modified: 2010-03-31

I am creating a program that generates 6 random numbers between 1 and 48; however, the 6 numbers must not match.  For example (4, 26, 5, 9, 47, 2);

I had success in generating random numbers, but at times my numbers would come out matching like so ( 4, 34, 5, 4, 32, 1).  In C programming I know you could simply use "srand(Time(NULL))" to eradicate this problem, but in Java I can't find a way to do this.  I want to avoid using many if statements.  

I was told to use two for loops, first one to generate my numbers, and the second one to check them.  My numbers are stored in an array, if that helps.  

If you have any suggestions I would appreciate them.  
0
Comment
Question by:ocha033
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 168 total points
ID: 8204917
easy way:
ArrayList al = new ArrayList();
for (int i=1;i<=48;i++) {
     al.add(new Integer(i));
}
Collections.shuffle(al);
for (int i=1;i<=6;i++) {
     System.out.print(((Integer) al.get(i)).toString() + " ");
}
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 8204925
what it does:

loads the numbers from 1 to 48 into an arraylist.
Shuffles the list.
prints out the first 6 items
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8205043
That's a good way. You can write

>>System.out.print(((Integer) al.get(i)).toString() + " ");
       
as

>>System.out.print(al.get(i) + " ");
0
 

Author Comment

by:ocha033
ID: 8206244
Thanks a lot.  I never would have thought to use an array with all my numbers and then simply displaying some of them.  That comes from the card shuffling programs too.  

Thanks.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8206279
If you wanted to conserve resources, this redundancy wouldn't be a good thing of course ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question