• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

getting random numbers

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And it's better if you explain your question further more...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i am not sure if this is the right way.


 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80083
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But myRandom.nextInt(i) will do it more elegantly. I agree with Jeff.
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Just checked the API docs.

Looks like I gotta break my habits
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic