random pairing of array elements, similar to schedule algorithm

Hi experts!
im new to java and trying to create a program that takes a list of names and pairs them together for a holiday gift swap; the elements should only be used once ( give, receive)

e.g.

list of 5 names :

1 has 3
3 has 2
2 has 1
4 has 5
5 has 4

any help is greatly appreciated.

import java.util.Random;
 
public class NamePairing {
 
	public static void main(String[] args) {
		
 
		Random generator = new Random();
		int fTen,lTen;
		
		   String Fnames[] = new String[5];
		   String Lnames[] = new String[7];
		   
		// List of giving names.
	        Fnames[0] = "name1";
	        Fnames[1] = "name2";
	        Fnames[2] = "name3";
	        Fnames[3] = "name4";
	        Fnames[4] = "name5";
	        
	      
	    // List of receiving
	        Lnames[0] = "name1 ";
	        Lnames[1] = "name1 ";
	        Lnames[2] = "name1 ";
	        Lnames[3] = "name1 ";
	        Lnames[4] = "name1 ";
	        
		   	   
		   for(int i = 0; i < Fnames.length && i <= Lnames.length;i++)
		   {
			   fTen = generator.nextInt(4)+1;
			   lTen = generator.nextInt(4)+1;
			   System.out.println(Fnames[fTen] + " has" + Lnames[lTen]);	
			   	if (Fnames[i] == Lnames[i])
			   		i--;
			   
		   }
		   
		
		
	}

Open in new window

everetjoAsked:
Who is Participating?
 
szigetiConnect With a Mentor Commented:
I noticed that your example allowed mutual pairs, so I modified the code to allow that version:
e.g.
Julia is with Susan
Susan is with Julia

Output:
John is with Arnold
Bob is with John
Susan is with Julia
Arnold is with Bob
Julia is with Susan

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class NamePairing
{
    public static void main(String[] args)
    {
    	List fNames = new ArrayList();
    	fNames.add("John");
        fNames.add("Bob");
        fNames.add("Susan");
        fNames.add("Arnold");
        fNames.add("Julia");
        
        List copy = new ArrayList(fNames);
        Random generator = new Random();
                   
        for(int i = 0, remaining = copy.size(); i < fNames.size(); i++, remaining--)
        {
        	int offset = generator.nextInt(remaining);
        	
        	// If we accidentally pick ourselves, get the next person
        	if(fNames.get(i).equals(copy.get(offset)))
        	{
        		offset = (offset + 1) % remaining; 
        	}
        	          	
        	System.out.println(fNames.get(i) +
        			" is with " + copy.remove(offset));
        }
    }
}

Open in new window

0
 
Kevin CrossChief Technology OfficerCommented:
Just remember that Random#nextInt(int n) returns random value between 0 and n-1 inclusive -- n is not included -- therefore, for an array with 5 elements you probably want nextInt(5) since 0-4 are the valid indices.  If you use nextInt(4)+1 you will only get values 1-4, so you will not use index 0.

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Random.html#nextInt(int)

For more scalable code, you could use nextInt(FName.length) instead of hardcoding.
0
 
szigetiCommented:
Here ya go, take a look at this approach which involves taking the list, and shifting it by a random number and matching the original list against the shifted list. This guarantees the outcome is random, and that you dont have cases such as where "John is with Susan" and "Susan is with John":

John is with Susan
Bob is with Arnold
Susan is with John
Arnold is with Julia
Julia is with Bob

Sample outputs:
John is with Susan
Bob is with Arnold
Susan is with Julia
Arnold is with John
Julia is with Bob


John is with Bob
Bob is with Susan
Susan is with Arnold
Arnold is with Julia
Julia is with John



import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
public class NamePairing {
 
        public static void main(String[] args)
        {
        	List fNames = new ArrayList();
        	List lNames = new ArrayList();
 
            fNames.add("John");
            fNames.add("Bob");
            fNames.add("Susan");
            fNames.add("Arnold");
            fNames.add("Julia");
            
            Random generator = new Random();
            int offset = generator.nextInt(fNames.size() - 1) + 1;
            
            for(int i = 0; i < fNames.size(); i++)
            {
            	System.out.println(fNames.get(i) +
            			" is with " + fNames.get((i + offset) % fNames.size()));
            }
        }
}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
szigetiCommented:
Btw. it works by creating a copy of the original list, repeatedly picking a pair (if it accidentally picks itself, gets the subsequent pair), and removing each pair from the copy per loop cycle to avoid a person having multiple pairs.
0
 
everetjoAuthor Commented:
This is great stuff !!  Sorry so late on the reply.

0
 
everetjoAuthor Commented:
perfect!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.