• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Assigning random numbers to an array

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You can't have everything. Where would you put it?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic