0

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

4
Contributors
5
Replies
6
Views
12 Years
Discussion Span
Last Post by server_crash
0

hi v2_vehooi

check out the following code, is that your requirement?

class RandomGen
{
	public static void main(String args[])
	{
		int i=(int)(100+((int)Math.round(100*Math.random())));
		System.out.println(i);
	}
}

regards
srinivas

0

Hi Cheenu78,

I think your code should have helped v2_vehooi!!!

Regards,
Kamal

0

hi v2_vehooi

check out the following code, is that your requirement?

class RandomGen
{
	public static void main(String args[])
	{
		int i=(int)(100+((int)Math.round(100*Math.random())));
		System.out.println(i);
	}
}

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.