File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes getting random numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getting random numbers" Watch "getting random numbers" New topic
Author

getting random numbers

sudde gameeef
Greenhorn

Joined: Mar 21, 2013
Posts: 19
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/>





if()
System.out.println("random 1");
else if()
System.out.println("random 2");
else if()
System.out.println("random 3");
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 172
    
    1

After you have got the random numbers, I think you could use a switch rather than an if statement..


Ramesh-X
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 172
    
    1

And it's better if you explain your question further more...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7779
    
  21

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
sudde gameeef
Greenhorn

Joined: Mar 21, 2013
Posts: 19


i am not sure if this is the right way.


Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 172
    
    1

Your second code is fine. But you have written lots of unnecessary codes in you first code.



This will give the same result as in yours. But this contains no unnecessary codes...


If you use code as in your 2nd example, it better to use switch..case statement!!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10360
    
    8

sudde gameeef wrote:

Like you were requested before, please check your private messages for an important administrative matter. Please note, this is not optional.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

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 hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 172
    
    1

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..!!!

Thank you..!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

I wouldn't call it a great mistake. But with computers and programs you always have to be really careful, especially with corner cases.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Yes. Maybe I'm more habitual with Math.random()

Generally, if I need random number ranging from 0 to n, I would go for
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38788
    
  23
But myRandom.nextInt(i) will do it more elegantly. I agree with Jeff.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Yes. Just checked the API docs.

Looks like I gotta break my habits
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting random numbers