Actually, there is no guarantee that you won't get the same number in the array twice. Math.random() uses an object of the class java.util.Random, which is a pseudo-random number generator. See the Javadoc for java.util.Random.
Yeah, it may have been a good idea to show the array. And the init of the nums array. As it is not that clear what the code does.
Now I understand what it does, but I don't know how it does it. For example, how does it make sure the same number isn't assigned in the array twice?
Assuming that the loop is just a simple loop that indexes through the array, it doesn't matter if the same random number appears twice. Take a look at the code again -- it is not assigning the random number to the array. The random number is used as an index to determine which array index will be swapped, with the current index.
Think of it as shuffling a deck of cards. It doesn't matter if the same index appears twice. At the end, the cards will still be unique. Bad random numbers just means that the cards may not be well shuffled -- not that you will get duplicate cards.
Ohhh! That's cleared it up nicely, now I understand it, thanks! Can't believe I never thought of it like that...
See at the moment it's not the learning of the syntax which is proving to be difficult to learn, I'm understanding it quite well and memorising a lot of it. It's actually using the syntax to make clever algorithms that do stuff like this (i.e structuring it all in a way which creates an efficient program).
Is it because I'm not some mathematics whizz that's great with numbers, or simply lack of experience? Will I be better at thinking up how to create clever algorithms as I continue to read and get deeper in to Java?
You can't have everything. Where would you put it?