i am making a game where i need to create object only onces randomly. <br/>
i can get random number by random object. but not sure how to put in if statment. <br/>

sudde gameeef wrote:i am making a game where i need to create object only onces randomly. <br/>
i can get random number by random object. but not sure how to put in if statment. <br/>

I'm not sure I understand the question. Are you sure you've read and understood the java.util.Random API? (and BTW, it's 'Random', not 'random' - that sort of stuff is very important in Java).

The reason I ask is that I don't see any attempt to call one of its methods in your code.

Normally, I'd suggest using the online tutorials; but unfortunately they still seem to be stuck on using Math.random(), which is vastly inferior to the Random class. This page seems to provide a quick intro with a few examples though.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here

If you need to create random number only once (and especially if you are not dealing with a series of random numbers), I would suggest to use 'random' method of 'Math' class.

Ramesh Pramuditha Rathnayake wrote:This will give the same result as in yours. But this contains no unnecessary codes...

Almost the same. The original code didn't print anything if x was exactly equal to 0, 100 or 200. Also, the condition (x < 300) is unnecessary, because r.nextInt(300) will always produce a number less than 300.

Jesper de Jong wrote:Almost the same. The original code didn't print anything if x was exactly equal to 0, 100 or 200. Also, the condition (x < 300) is unnecessary, because r.nextInt(300) will always produce a number less than 300.

You are right..!
I have done a greate mistake..!!!

Anayonkar Shivalkar wrote:If you need to create random number only once (and especially if you are not dealing with a series of random numbers), I would suggest to use 'random' method of 'Math' class.

It's quite easier than 'Random' class itself.

I disagree with that. I don't think Math.random() is any easier to use. And particularly if you need an int, as the OP does here, Random is somewhat easier. And