By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,311 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,311 IT Pros & Developers. It's quick & easy.

Random Permutation List Generator

P: 2
Hello.
I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work?

THIS IS WHAT I NEED TO DO:


Professor Snoop wants a program that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the numbers 1,2,3: 123, 132, 231, 213, 312, and 321.



To generate a random permutation, you need to fill an ArrayList with the numbers 1 to 10 so that no two entries of the array have the same contents.
You could do it by brute force, by calling Random.nextInt ( ) until it produces a value that is not yet in the array. Instead, you should implement a smart method.
Make a second ArrayList and fill it with the numbers 1 to 10.
Then pick one of those at random, remove it, and append it to the permutation ArrayList. Repeat ten times.
Implement a class PermutationGenerator with the following method:

ArrayList nextPermutation

The output will consist of 10 lists of random permutations of the numbers 1 through 10.
Example output is shown below:

Random Permutation List Generator
List 1: 4 6 8 1 9 7 10 5 3 2
List 2: 6 8 1 7 3 4 9 10 5 2
List 3: 2 4 9 6 8 1 10 5 7 3
List 4: 8 5 4 3 2 9 6 7 1 10
List 5: 10 3 2 6 8 9 5 7 4 1
List 6: 9 10 3 2 1 5 6 8 4 7
List 7: 3 8 5 9 4 2 10 1 6 7
List 8: 3 2 4 5 7 6 9 8 10 1
List 9: 4 1 5 10 8 3 6 2 7 9
List 10: 3 5 2 4 1 7 9 6 8 10


THIS IS WHAT I HAVE SO FAR:


Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * Write a description of class PermutationGenerator here.
  3.  * 
  4.  * @author (your name) 
  5.  * @version (a version number or a date)
  6.  */
  7. import java.util.Random;
  8. import java.util.ArrayList;
  9.  
  10. public class PermutationGenerator
  11. {
  12.     public static void main(String[]args) 
  13.     {
  14.         for(int i = 1; i <= 10; i++)
  15.         {
  16.             System.out.print("List " + i + ":");
  17.         }
  18.  
  19.         class PermutationGenerator
  20.         {
  21.             //create an array of #s from 1-10 without 2 entries
  22.             // having the same content.
  23.             private ArrayList<Integer>nums = new ArrayList<Integer>();
  24.             {
  25.                 String[] Numbers = new String[11];
  26.                 String[] randomNum = {"", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};  
  27.  
  28.                 int i = 10;
  29.                 int n = 10;
  30.                 /*creates an array for the randomly selected numbers*/
  31.             }
  32.  
  33.             private ArrayList<Integer>permutation = new ArrayList<Integer>();
  34.             {
  35.                 String Numbers = randomNum.Random.nextInt();
  36.             }
  37.  
  38.         public int PermutationGenerator();
  39.         {
  40.             for(int i = 0; i < 10; i++)
  41.             {
  42.                 nums.add(i + 1);
  43.             }        
  44.  
  45.         Random generator = new Random();
  46.         {
  47.             for (int n = 0; n < 10; n++)
  48.  
  49.             int n = randomNum.nextInt(nums.size());
  50.             permutation.add(nums.get(d));
  51.         }
  52.     }
  53. }
  54.  
Please help!!!

Thank You.
Jan 24 '08 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
1.) Use code tags when posting code.
2.) What is your exact problem? Are you getting an error/exception incorrect output?
Jan 24 '08 #2

BigDaddyLH
Expert 100+
P: 1,216
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Jan 24 '08 #3

BigDaddyLH
Expert 100+
P: 1,216
badcrusher10,

Your code is a long way from compiling. It may be easier just to start over and to do this: compile early and compile often. Write just the outline of the file:

Expand|Select|Wrap|Line Numbers
  1. import ...
  2. import ...
  3.  
  4. public class Soemthing {
  5.  
  6. }
And compile that. Add one method and compile that. For example:
Expand|Select|Wrap|Line Numbers
  1. public class Something {
  2.     public static void main(String[] args) {
  3.         Something app = new Something("hello world");
  4.     }
  5. }
This generates an error because there is no such constructor, so I add the skeleton of one:
Expand|Select|Wrap|Line Numbers
  1. public class Something {
  2.     public static void main(String[] args) {
  3.         Something app = new Something("hello world");
  4.     }
  5.  
  6.     public Something(String message) {
  7.     }
  8. }
Now it compiles and I'm ready to add the next small thing, perhaps to implement the constructor properly. Fix syntax errors before pressing on. Don't try to enter everything at once.
Jan 24 '08 #4

Expert 10K+
P: 11,448
Also have a look at this little article.

kind regards,

Jos
Jan 24 '08 #5

P: 2
Thanks for all your help, but I still don't understand how to write and Random Permutation List Generator. Could someone please explain to me how to write it using Array Lists??


Thank You.
Jan 28 '08 #6

BigDaddyLH
Expert 100+
P: 1,216
Thanks for all your help, but I still don't understand how to write and Random Permutation List Generator. Could someone please explain to me how to write it using Array Lists??
You original assignment spelled out a solution in some detail. If you still have questions, you should post code and ask a specific question about it.
Jan 28 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.