?
Solved

random pairing of array elements, similar to schedule algorithm

Posted on 2008-11-11
6
Medium Priority
?
809 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:everetjo
  • 3
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22933604
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
 
LVL 2

Expert Comment

by:szigeti
ID: 22953377
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
 
LVL 2

Accepted Solution

by:
szigeti earned 2000 total points
ID: 22953674
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 2

Expert Comment

by:szigeti
ID: 22953705
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
 

Author Comment

by:everetjo
ID: 22988157
This is great stuff !!  Sorry so late on the reply.

0
 

Author Closing Comment

by:everetjo
ID: 31515671
perfect!
0

Featured Post

Industry Leaders: 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

830 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