473,729 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random Permutation List Generator

2 New Member
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 PermutationGene rator 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 11746
r035198x
13,262 MVP
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
Also have a look at this little article.

kind regards,

Jos
Jan 24 '08 #5
badcrusher10
2 New Member
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 Recognized Expert Top Contributor
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
3701
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 specified range), random strings (specifying allowed characters and a distribution of lengths), or choose randomly between several generators (for better control of the distribution of values). Is there any library for this sort of thing in Python?...
10
2501
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 spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone can help it would be greatly appreaciated, I have tried many of
8
2476
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) maxRange=1000 the output would be:
10
5635
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 = lst for x in permute( lst ): yield head + x
70
6264
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 using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not using rand() if you want more than approx. UINT_MAX total different sequences, and so on. So I...
16
12077
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 numbers using asp.net? I know about the random namespace within .net, but I need a reference to some code that do the similar stated function above. Plus If you have any coding practice ideas for the above defined project please share them.
19
3082
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 achieve the sort, I'd say the answer is yes. But I don't feel quite confortable with the intuition... can anyone think of a more solid argumentation ?
12
3225
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
13
2243
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): for i in range(len(perm)+1):
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9285
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9151
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8155
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6025
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.