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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
szigetiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.