Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Pick random objects from array/arraylist. Newbie.

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Pick random objects from array/arraylist. Newbie.

    Hey all!

    The only thing left that bugs me in my into java class is this - picking random objects from arrays or arraylists or wherever. After that I'm sailing free and moving to more advanced stuff

    Alright people, please give me tips or hints about this thing, as I really want to improve my object to int/string/double/etc. skills. The bottom line is the use of Random generator - not Math.methods or similar. So how can I tune this thing?

    Assume following thing, example with objects of Employee class.
    Code with comments:

    import java.util.*;
    import java.util.Random;
     
    class Employee{
    	private int salary;
     
    	public void setSalary(int s){
    		s= salary;
    	}
    	public int getSalary(){
    		return salary;
    	}
    }
     
    class rand{
    	public Employee [] get(Employee [] arr){
    		Employee [] arr2 = new Employee [3]; //three (or whatever) values.
    		Random rand = new Random(); //setting up the Random generator - How to with Objects??
    									//unique numbers can be fixed through while loops by adding to the new array manually - no problem here.
     
    	}
    }
     
    public class Job{
    	public static void main(String [] args){
    		Employee [] list = new Employee [5]; //Lets say 5 (or whatever).
    		Employee e1 = new Employee();
    		Employee e2 = new Employee();
    		Employee e3 = new Employee();
    		Employee e4 = new Employee();
    		Employee e5 = new Employee();
     
    		e1.setSalary(10000);
    		e2.setSalary(2000);
    		e3.setSalary(15000);
    		e4.setSalary(18000);
    		e5.setSalary(23000);
     
    		list[0] = e1;
    		list[1] = e2;
    		list[2] = e3;
    		list[3] = e4;
    		list[4] = e5;//Yep, doing it with lots of code :)
     
    		//Here I will call on the class rand.get(list) - so it will make wonders and I can do whatever I want with the new array (or whatever) :P
     
    	}
    }

    Any suggestions will be greatly appreciated! But people, focus on the Random generator please, I know that there are other methods that will work besides this.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,260
    Thanks
    176
    Thanked 818 Times in 761 Posts
    Blog Entries
    5

    Default Re: Pick random objects from array/arraylist. Newbie.

    To get a random int between 0 and n-1, multiply the next floating point random number times n, casting to an int and getting its absolute value. To use this to find a random object in an array or list, n would be the size of said array or list.
    Last edited by copeg; October 23rd, 2010 at 02:08 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pick random objects from array/arraylist. Newbie.

    copeg

    Your answer was a little advanced for me, but I took the bits and working on it.. Thank you! Here's what I've come up with. Example of a lottery drawing:

    Code:
    import java.util.*;
    import java.util.Random;
    import java.util.ArrayList;
     
    class Ticket{
    	private int id;
     
    	public void setid(int i){
    		i=id;
    	}
    	public int getid(){
    		return id;
    	}
    }
     
    class pickRand{
    	public static Ticket [] get(ArrayList <Ticket> jipp){
    		Ticket [] rst = new Ticket [5];
    		Random rand = new Random();
     
    		for(int i=0;i<rst.length;i++){
    		Ticket uniq = jipp.remove(rand.nextInt(jipp.size()));
    		rst[i] = uniq;
    		}
    		return rst;
    	}
    }
     
    public class Account{
    	public static void main(String []args){
     
    		int[] id = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    		ArrayList <Ticket> drw = new ArrayList <Ticket>();
    			      for(int i = 0; i < id.length; i++) {
    			          drw.add(new Ticket());
    			          drw.get(0).setid(id[i]);
    			      }
    			      Ticket [] results = pickRand.get(drw);
    			      System.out.println(results[0] + " " + results[1] + " " + results[2] + " " + results[3] + " " + results [4]);	      
    	}
    }

    It does return random Tickets, I still od wonder if can fix that rand.sentence in another way...

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Pick random objects from array/arraylist. Newbie.

    After wrapping my head around the problem I've come up with the solution for unique objects using random generator:

    class pickRand{
        public static Ticket [] get(ArrayList <Ticket> jipp){
            Ticket [] rst = new Ticket [5];
            Random rand = new Random();
     
            for(int i=0;i<rst.length;i++){
            	Ticket uniq = jipp.remove(rand.nextInt(jipp.size()));
            	int a = 0;
            	do{
            		a=0;
            		for(int j =0;j<i;j++){
            			if(rst[j].equals(uniq)){
            				a =1;
            			}
            		}
            	}while(a==1);
            	rst[i] = uniq;
            }
            return rst;
        }
    }

    Hope this helps for someone who's looking for answers. The main thing is to check the array, and this can be done in several different ways, but here's one for ya.

    This can also be made FAR more simpler using shuffle or Math classes/objects.

    Thanks for feedback!

Similar Threads

  1. Minimum value of objects in ArrayList
    By Sputnik in forum Loops & Control Statements
    Replies: 2
    Last Post: October 23rd, 2010, 01:20 PM
  2. [SOLVED] Problem with Ordering an Arraylist of Comparable Objects.
    By Faz in forum Collections and Generics
    Replies: 8
    Last Post: June 16th, 2010, 06:36 PM
  3. looping through an ArrayList of objects with their own attributes
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: April 2nd, 2010, 06:15 AM
  4. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 07:01 PM
  5. How can i store ArrayList objects in Access database
    By frankycool in forum JDBC & Databases
    Replies: 0
    Last Post: November 3rd, 2009, 11:44 PM