• Post Reply Bookmark Topic Watch Topic
  • New Topic

random sequence generation  RSS feed

 
Taylor Thibodeaux
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have a program for a project that permutes a random series of numbers using Java's Rand function. Basically I use a seed and a number of items I want in the arrangement of numbers, and the program makes a permutation with no two numbers being repeated. Only arrays can be applied to it, so I've been hard at work finding a solution. Here's the code so far:



So basically I'm wanting the program to make a randomized list of numbers from the number of items I pass to the Permute method with no duplicates. I'm having some level of success with what I have written, as it gives me a randomized list when printing the output, but for some reason the first for statement in code never terminates fully, but instead runs indefinitely when generating the last integer. For example, if I want to put 10 with a seed of 0 into it and make a list from 0-9, it will print 74283510, which is only 8 different integers. permutation[0] is manually set at the beginning of the method, which accounts for one more, but that's still only a list of 9, so I'm just wondering why the last integer is not being generated and why the program keeps looping and not terminating? I'm know for sure it's something I'm overlooking. Sorry for being so verbose, but I can clarify something if needed. Thanks!
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you look at that particular example you'll notice that 9 is never selected. I believe that if you try several other examples with different seeds you will always find that 9 is never selected. There's a reason for that -- you wrote the code so that 9 can never be chosen.

By the way you don't need that special code to set the value of permutation[0]. You will find that if you don't bother with that, but just start your loop at 0 instead of 1, it will be assigned a value just like all the other elements of the array.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!