Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to genrate a random number inside the range

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I want to generate a random number for a specific range
like in between 20 to 70 no.s


cinux
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sai,
I had a same kind a requirement in one of my modules. I used the Random class for this. check this code

num will have the random number between 30 - 40
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that there is even a method nextInt(int n) on the Random class.
 
Sri Ram
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Notice that there is even a method nextInt(int n) on the Random class.


But doesnt this method generate random number between 0 and specified value.

regards
Sriram
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> But doesnt this method generate random number between 0 and specified value.

So add the lower bound. Is this really an advanced question?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrechtsen:
Is this really an advanced question?


No, it isn't. Moving to intermediate...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic