• Post Reply Bookmark Topic Watch Topic
  • New Topic

Coding Thoughts  RSS feed

 
Jason Venter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of my assignments asked me to specify a range when using Random.nextInt(). I assumed I could do something like this:


But that did not work. I found the solution was to do determine the number of possible values and then add the minimum value to that result:


This makes sense to me, but I couldn't figure this out on my own and needed to lookup a solution. So my questions is this: is there any process to help when solving problems like this or does this getter better the more you program or is this something I should have known?

Thank you.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some people can figure these things out but don't worry about not knowing it. These are idioms that you'll get used to as you gain more experience. It's just like learning any new language, there will be idioms that you'll just pick up as you keep learning how to speak the language more fluently.
 
Jason Venter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Some people can figure these things out but don't worry about not knowing it. These are idioms that you'll get used to as you gain more experience. It's just like learning any new language, there will be idioms that you'll just pick up as you keep learning how to speak the language more fluently.


Thanks @Junilu. Can you recommend any resources to get up to speed with these type of idioms?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Effective Java by Joshua Bloch probably is the best book as far idioms is concerned. Clean Code by Robert Martin would be another book to keep handy. Just keep practicing and reading other people's code and you'll start recognizing common idioms.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe they should have written a method like that in the Random class. But they didn't. There is a method which creates a Stream with minimum and maximum, but not to get a single value. I presume you know you will get values between 10 and 20 inclusive from your formula.
 
Jason Venter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Effective Java by Joshua Bloch probably is the best book as far idioms is concerned. Clean Code by Robert Martin would be another book to keep handy. Just keep practicing and reading other people's code and you'll start recognizing common idioms.


Thanks! I'll check out those books and keep practicing/reading!
 
Jason Venter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Maybe they should have written a method like that in the Random class. But they didn't. There is a method which creates a Stream with minimum and maximum, but not to get a single value. I presume you know you will get values between 10 and 20 inclusive from your formula.


Yes, that's the expected value range.

I tried the min/max range (coming from a python background).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!