• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting random numbers

 
sudde gameeef
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you have got the random numbers, I think you could use a switch rather than an if statement..
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it's better if you explain your question further more...
 
Winston Gutkowski
Bartender
Pie
Posts: 10103
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sudde gameeef
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i am not sure if this is the right way.


 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sudde gameeef wrote:

Like you were requested before, please check your private messages for an important administrative matter. Please note, this is not optional.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But myRandom.nextInt(i) will do it more elegantly. I agree with Jeff.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Just checked the API docs.

Looks like I gotta break my habits
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic