944,052 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1571
  • Java RSS
Jul 13th, 2005
0

regarding the random generated

Expand Post »
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??????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
v2_vehooi is offline Offline
6 posts
since Jun 2005
Jul 13th, 2005
0

Re: regarding the random generated

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
Reputation Points: 10
Solved Threads: 0
Light Poster
cheenu78 is offline Offline
45 posts
since Jun 2005
Jul 13th, 2005
0

Re: regarding the random generated

Hi Cheenu78,

I think your code should have helped v2_vehooi!!!

Regards,
Kamal
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lamak is offline Offline
1 posts
since Jul 2005
Jul 13th, 2005
0

Re: regarding the random generated

I would suggest adding one to the end of the generated number or else you wont ever reach 200.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jul 17th, 2005
0

Re: regarding the random generated

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
v2_vehooi is offline Offline
6 posts
since Jun 2005
Jul 17th, 2005
0

Re: regarding the random generated

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
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Is it possible to start this special thread in paintComponent() method ?
Next Thread in Java Forum Timeline: Where is this API??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC