Solved

# random pairing of array elements, similar to schedule algorithm

Posted on 2008-11-11
718 Views
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--;

}

}
``````
0
Question by:everetjo

LVL 59

Expert Comment

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

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();

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()));

}

}

}
``````
0

LVL 2

Accepted Solution

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();

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));

}

}

}
``````
0

LVL 2

Expert Comment

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

This is great stuff !!  Sorry so late on the reply.

0

Author Closing Comment

perfect!
0

## Featured Post

### Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …