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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# Any easy explanation for randoms please

Greenhorn
Posts: 7
I'm analyzing this code but find it hard to understand the random section. Whats the purpose of this?

Saloon Keeper
Posts: 4747
117
• 1
The purpose apparently is to select a random word from each of the word lists. If you haven't done so yet, start by reading the javadocs of Math.random() so you know what it returns. Maybe that makes it clearer what values rand1/2/3 can take - they are being used as indexes into arrays, after all, so they can't take on just any old value.

Ranch Hand
Posts: 570
3
• 1
Value for arr1Length is 5.
It means generating a number between 1 to 4.

Marshal
Posts: 4465
284
Also, for the purposes of copyright and correct attribution, please state where you have gotten this code from?

Sheriff
Posts: 12343
201
• 1

John Joe wrote:Value for arr1Length is 5.
It means generating a number between 1 to 4.

That's only half correct. The expression will give 0 to 4.

Marshal
Posts: 60136
188
Welcome to the Ranch

What your program doesn's tell you is that there is a better way to get a “random” number in the range 0...n − 1. There is a detailed discussion about that here. That was before Java8 which has methods for getting multiple “random” numbers in a Stream.

Saloon Keeper
Posts: 4765
52
Is the same as this, using the Random class.

Campbell Ritchie
Marshal
Posts: 60136
188
• 1
But using a Random object is much less error‑prone and may run slightly faster because you don't need arithmetic to turn two ints into a double and then do more arithmetic to turn it back into an int.

John Joe
Ranch Hand
Posts: 570
3

Junilu Lacar wrote:That's only half correct. The expression will give 0 to 4.

Thanks for the correction !

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.