Results 1 to 2 of 2
Thread: random array permutation
 11152012, 12:16 AM #1Member
 Join Date
 Jul 2012
 Posts
 93
 Rep Power
 0
random array permutation
hey,
I have been working on this bonus question due tomorrow for more than 4 hours total. This a just a part but I am not making good time with this and I am getting frustrated.
here is part of the question:
Write a function int[] RandomPermutation(int n) to create a random permutation of {0, 1, 2, ×××, (n − 1)}.
1. Create the initialize array nums[] = [0, 1, 2, ..., (n1)].
2. Create a random number m in the range 0 to i.
3. Exchange nums[m] and nums[i].
4. Repeat steps (2)(3) for i = n1, n2, ..., 1.
here is my code:
Java Code:package bonus; import java.util.Random; public class Bonus { public int[] RandomPermutation(int n){ int[] nums = new int[n]; Random rand = new Random(0); for (int i = 0; i < n; i++) { nums[i] = i; } for (int i = n  1; i > 0; i) { int m = rand.nextInt(i); System.out.println(m); System.out.println(nums[m] + " " + i); } return nums; } public static void main(String[] args) { Bonus permutation = new Bonus(); permutation.RandomPermutation(10); } }
I am about to take a break but i will probably be working on this for the rest of the evening.
OK, I think I got steps one two and four taken care of here. Now I need to implement step 3.Last edited by jwl; 11152012 at 01:55 AM.
 11152012, 08:01 PM #2
Similar Threads

Using a "Random" method to print out random index of Array List?
By RarkMowe in forum New To JavaReplies: 5Last Post: 10242012, 09:17 PM 
Need a help in two dimentaional permutation
By venk123 in forum New To JavaReplies: 7Last Post: 08062011, 01:28 AM 
Permutation
By Claymz in forum New To JavaReplies: 4Last Post: 06012011, 02:27 PM 
Mapping of variables to values (permutation with repetition?)
By electra in forum Advanced JavaReplies: 3Last Post: 08242009, 07:39 PM 
How to get a random value in an array
By Franneldort in forum New To JavaReplies: 21Last Post: 11012008, 02:42 PM
Bookmarks