File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assigning random numbers to an array

 
Andy Joness
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
 
Andy Joness
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic