954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

regarding the random generated

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??????

v2_vehooi
Newbie Poster
6 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

cheenu78
Light Poster
45 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

Hi Cheenu78,

I think your code should have helped v2_vehooi!!!

Regards,
Kamal

lamak
Newbie Poster
1 post since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

I would suggest adding one to the end of the generated number or else you wont ever reach 200.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

v2_vehooi
Newbie Poster
6 posts since Jun 2005
Reputation Points: 10
Solved Threads: 0
 

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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You