regarding the random generated

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2005
Posts: 6
Reputation: v2_vehooi is an unknown quantity at this point 
Solved Threads: 0
v2_vehooi v2_vehooi is offline Offline
Newbie Poster

regarding the random generated

 
0
  #1
Jul 13th, 2005
someone can teach me about the random genrated?
let say i would like to generate a random number between 100 to 200 and store it into one integer variable, what is the code??????
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 45
Reputation: cheenu78 is an unknown quantity at this point 
Solved Threads: 0
cheenu78's Avatar
cheenu78 cheenu78 is offline Offline
Light Poster

Re: regarding the random generated

 
0
  #2
Jul 13th, 2005
hi v2_vehooi

check out the following code, is that your requirement?
[HTML] class RandomGen
{
public static void main(String args[])
{
int i=(int)(100+((int)Math.round(100*Math.random())));
System.out.println(i);
}
}
[/HTML]
regards
srinivas
We come to love not by finding a perfect person, but by learning to see an imperfect person perfectly.

-Sam Keen, from To Love and Be Loved
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1
Reputation: lamak is an unknown quantity at this point 
Solved Threads: 0
lamak lamak is offline Offline
Newbie Poster

Re: regarding the random generated

 
0
  #3
Jul 13th, 2005
Hi Cheenu78,

I think your code should have helped v2_vehooi!!!

Regards,
Kamal
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: regarding the random generated

 
0
  #4
Jul 13th, 2005
I would suggest adding one to the end of the generated number or else you wont ever reach 200.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 6
Reputation: v2_vehooi is an unknown quantity at this point 
Solved Threads: 0
v2_vehooi v2_vehooi is offline Offline
Newbie Poster

Re: regarding the random generated

 
0
  #5
Jul 17th, 2005
Originally Posted by cheenu78
hi v2_vehooi

check out the following code, is that your requirement?
[HTML] class RandomGen
{
public static void main(String args[])
{
int i=(int)(100+((int)Math.round(100*Math.random())));
System.out.println(i);
}
}
[/HTML]
regards
srinivas
thank you very much
this is what i need for the output
but can u explain to me more detail about this code how it run?
like the Math.round, why u need use this? and how u make the range between 100 to 200? if between this range i need to include the 200 so how
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: regarding the random generated

 
0
  #6
Jul 17th, 2005
The best way is to add one to the end. You don't need the round method; A better choice would be the Math.ceil() method since it rounds up.


int i=(int)(101+((int)100*Math.random()));

or


int i=(int)(100+((int)101*Math.random()));

or


int i=(int)(100+((int)100*Math.random()))+1; //My choice
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1459 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC