Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: LAST issue! Random permutation array!

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Exclamation LAST issue! Random permutation array!

    I have the array printing out just 0's. I am not sure what i have to input to make it output random digits. I want random digits 0-9, I am not sure what to do. This is the last program i need help on. Thank you for any help!

    /**
       This class prints 5 permutations of the numbers 1 through 10.
    */
    public class PermutationPrinter
    {  
       public static void main(String[] args)
       {  
          PermutationGenerator gen = new PermutationGenerator(10);      
     
          for (int i = 1; i <= 5; i++)
          {  
             for (int n : gen.nextPermutation())
                System.out.print(" " +  n);
                }
       }
    }

    import java.util.Random;
    import java.util.*;
    /**
       This class generates permutations of a sequence of integers 
       1...length.
    */
    public class PermutationGenerator
    {   
      ArrayList<Integer> number = new ArrayList<Integer>();
      int size; 
     
       /**
          Construct a PermutationGenerator object.
          @param length the length of the permutations generated
          by this generator.
       */
       public PermutationGenerator(int length)
       {	size = length;
       for (int i = 0; i <= length; i++){
       number.add(i);
       } 
       }
       /**
          Gets the next permutation.
          @return the array containing the next permutation
       */
       public int[] nextPermutation()
       { 
          int[] x = new int[size];
    	  for( int i = 0; i<size; i++){
    		Random k = new Random();
    		int y = k.nextInt(number.size());
     
    	  }
    	  return x;
    	}
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: LAST issue! Random permutation array!

    In your nextPermutation() method, you are never adding the values from the for loop to your int array. So you are just creating a new array and passing on that empty array, which would contain all 0s

  3. The Following User Says Thank You to Parranoia For This Useful Post:

    bankoscarpa (May 5th, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    17
    My Mood
    Stressed
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: LAST issue! Random permutation array!

    That makes sense actually, but how would i go about doing that?

  5. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: LAST issue! Random permutation array!

    Quote Originally Posted by bankoscarpa View Post
    That makes sense actually, but how would i go about doing that?
    You can just make an assignment statement after the creation of the random number (y). You should assign y to an index of your array.

  6. The Following User Says Thank You to andreas90 For This Useful Post:

    bankoscarpa (May 5th, 2012)

  7. #5
    Member
    Join Date
    Apr 2012
    Posts
    152
    Thanks
    0
    Thanked 22 Times in 22 Posts

    Default Re: LAST issue! Random permutation array!

    Something like
    x[i] = y;

  8. The Following User Says Thank You to Parranoia For This Useful Post:

    bankoscarpa (May 5th, 2012)

Similar Threads

  1. Picking Random Element from Array
    By Blackrabbitjack in forum Object Oriented Programming
    Replies: 4
    Last Post: March 21st, 2012, 03:09 PM
  2. [SOLVED] Array random numbers
    By nitwit3 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 20th, 2011, 07:05 PM
  3. random numbers in array please help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 12:54 AM
  4. [SOLVED] Permutation
    By Nahara in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 6th, 2010, 05:54 PM
  5. Returning Random Strings from an Array
    By cfmonster in forum Collections and Generics
    Replies: 3
    Last Post: September 8th, 2009, 11:13 PM