Generating a unique Random Generator in Java

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 734 Views - Last Post: 01 May 2013 - 12:46 AM Rate Topic: -----

#16 radarmanman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 30-April 13

Re: Generating a unique Random Generator in Java

Posted 30 April 2013 - 11:52 PM

I have two problems now. With the
for(int x=0;x<51;x++)
placed, I keep getting infinite loops when the 2d array is trying to add the values in. Other than that, I'm still trying to find a way to reference the arraylist in properly without it duplicating.

Also, do I need to make an abstract class for List now? I didn't know it was this difficult to reference the duplicator into my 2d array.
Was This Post Helpful? 0
  • +
  • -

#17 radarmanman  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 30-April 13

Re: Generating a unique Random Generator in Java

Posted 01 May 2013 - 12:38 AM

Sorry for double posting. I don't know where the edit button is on this forum. I figured it out.

Basically, instead of getting each individual value from the arraylist after referencing it in my table, I figured I had to remove each individual value that was added during the portion of the program that handled the duplication.

 grades[j] = myNumbers.remove((myNumbers.size()-1));


Which basically means that each iteration removes a value from the ArrayList and adds it as a parameter onto my linkedlist object. I didn't do a all out, throughout check of the output but I didn't not see any repititions for each row, which basically means that duplication is gone.

Student 1: 30 6 49 12 23 
Student 2: 7 36 17 14 41 
Student 3: 26 24 1 48 21 
Student 4: 39 44 27 40 28 
Student 5: 22 15 5 29 25 
Student 6: 37 19 10 46 8 
Student 7: 47 45 32 31 34 
Student 8: 38 9 3 18 13 
Student 9: 11 20 16 4 42 
Student 10: 33 43 35 2 50 



Once again I like to thank you for helping me. I'm gonna spend the next few days studying ArrayList and maybe integrate this into my data structure. Thank you so much, Gungir.
Was This Post Helpful? 1
  • +
  • -

#18 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Generating a unique Random Generator in Java

Posted 01 May 2013 - 12:46 AM

EDIT: Oh, I thought that you wanted a random value from the ArrayList. I don't know why I thought that. Derp.

Huh, would you look at that. After I looked at your solution, I looked up the documentation. Surely remove() has a void return type... But nope, it has a generic return type. It returns the object that you remove. Go figure, today I learned.

Now, you might get an IllegalArgumentException, which is fine. Just use a try block:
try {
	grades[j] = myNumbers.remove((myNumbers.size()-1));
} catch(IllegalArgumentException e) {
	e.printStackTrace();
}



---Original-Post----

Ahh, sorry. It was fine to begin with. I had a lapse in concentration.

The issue here is differentiating between values in the array, and the Index of the ArrayList.
For instance, if the value of index 48 is 47, we want the program to remove index 48, but it's trying to remove index 47.

We need to make that differentiation, by using an actual object to represent our Integer value (an Integer object, in fact)

The complete code
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Student
{
    private int grades, num;
    public Student(int num, int grades)
    {
        this.grades = grades;
        this.num = num;
    }

    public int getGrade()
    {
        return this.grades;
    }
    public int getNumber()
    {
        return this.num;
    }
}

public class myClass // put your own class name here, obviously
{	
	public static void main(String[] args)
    {
		List<Integer> myNumbers = new ArrayList<Integer>();
		Random randGen = new Random();
		int randNum=0;
		int lastNum;
		for(int x=0;x<50;x++)
		{
			lastNum = randNum;
			while(myNumbers.contains(randNum))
			{
				randNum = randGen.nextInt(50);
			}
			 
			myNumbers.add(randNum);
			System.out.println(randNum);
		}
		ArrayList<Student> list = new ArrayList<Student>();
		for(int i=0;i<=9;i++)
		{
			int[] grades = new int[5];
			for(int j=0;j<=4;j++)
			{
				//study the code within this FOR loop
				int temp = myNumbers.size() - 1;
				try {
					grades[j] = myNumbers.get(randGen.nextInt(temp));
				} catch(IllegalArgumentException e) {
					e.printStackTrace();
				}
				System.err.println(grades[j]);
				myNumbers.remove(new Integer(grades[j]));
				list.add(new Student(i+1,grades[j]));
			}				
		}
		for(Student student : list)
		    System.out.print("Student" + student.getNumber() +": " + student.getGrade());
	}
}



Further Reading:
- Oracle Docs > The try Block

This post has been edited by Gungnir: 01 May 2013 - 12:53 AM

Was This Post Helpful? 4
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2