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

Recommended Answers

All 5 Replies

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

Hi Cheenu78,

I think your code should have helped v2_vehooi!!!

Regards,
Kamal

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.