473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Random Permutation List Generator

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
6 11713
r035198x
13,262 8TB
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
1,216 Expert 1GB
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
1,216 Expert 1GB
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
JosAH
11,448 Expert 8TB
Also have a look at this little article.

kind regards,

Jos
Jan 24 '08 #5
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
1,216 Expert 1GB
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

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

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
10
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head =...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
19
by: Boris Borcic | last post by:
does x.sort(cmp = lambda x,y : cmp(random.random(),0.5)) pick a random shuffle of x with uniform distribution ? Intuitively, assuming list.sort() does a minimal number of comparisons to ...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
13
by: sillyhat | last post by:
Hello, can someone please help. I found the following code at http://code.activestate.com/recipes/252178/ def all_perms(str): if len(str) <=1: yield str else: for perm in all_perms(str):...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.