Results 1 to 2 of 2
Thread: Shuffling Help
- 10-20-2008, 10:04 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 1
- Rep Power
- 0
Shuffling Help
I have a bit of a problem. I'm trying to create a program that takes a user defined integer from the command line N and shuffles the integer from 0 through N.
The problem I have, is that the integers must not follow each other. Meaning, 4 does not follow 5, and 8 does not follow 7. I've created the program so that it shuffles the integers, then prints them out. However, I cannot seem to figure out the trouble I am having. Any help would be appreciated.
This is an example of what it currently prints out.Java Code:public class Shuffle { public static void main(String[] args) { int N = Integer.parseInt(args[0]); int[] song = new int[N]; for (int position = 0; position < N; position++) { int list = position; song[position] = list; } for (int current = 0; current < N; current++) { int swap = current + (int) (Math.random() * (N - current)); int tempSong = song[current]; song[current] = song[swap]; song[swap] = tempSong; System.out.print(song[current] + " "); } System.out.println(); } }
I would need to fix it so that 2, doesn't follow or come before 1. 6 before or after 7, etc.Java Code:java Shuffle 10 2 1 3 8 6 7 9 0 4 5
Thanks!
- 10-20-2008, 10:54 PM #2


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks