aspose file tools*
The moose likes Beginning Java and the fly likes Assigning random numbers to an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assigning random numbers to an array" Watch "Assigning random numbers to an array" New topic
Author

Assigning random numbers to an array

Andy Joness
Greenhorn

Joined: Sep 15, 2009
Posts: 24
Hey all, first post here.

I'm reading through a book and have come across something I really can't get my head around.



(this is within a loop btw)

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?

Cheers
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Andy:

Welcome to JavaRanch!

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.

John.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

(this is within a loop btw)



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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Andy Joness
Greenhorn

Joined: Sep 15, 2009
Posts: 24
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assigning random numbers to an array