Advertisement

[java] New Q: Random #'s in Java

Started by November 07, 2000 01:56 PM
3 comments, last by lupine 24 years, 2 months ago
I am coming from a different programing language and learning java. I think I am stating to "get it" If someone has time could you please post a simple java application program (start to finish) Which does the folowing: display a random # (1-6) on screen Thank You, and np if you don''t have time.
"do you like my helmut?"-yoghurt
Hello :

here it is (simple as that):

     public class SimpleApp {  //Main method  public static void main(String[] args) {     // ranNum is the Random number      int ranNum = (int)(1 + Math.random() * 6);      System.out.println(ranNum);  }}     


This is a simple java app that will display a random number (1-6) it will run as a console program (in dos) since you weren''t specific about whether it was a window(ed) app or console app.

JP.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
Advertisement
Thanks JP.
Seeing this code answers alot
of questions for me and having
it as a reference will also help
I have a follow up question.
You said this will run in DOS.
Being Java, wouldn''t it run
anywhere?
"do you like my helmut?"-yoghurt
What I meant it is a console application and i used DOS as an example of where the app will run. it could run in the linux console and others, etc. (it''s not going to generate a windowed app)

JP.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
There is another method you can use like this:

  // create new random object and seed it with timeRandom random = new Random(System.currentTimeMillis());// Returns a random number in the given rangeprivate int getRandomNum(int range){	return Math.abs(random.nextInt() % range);}  


- DanielMy homepage

This topic is closed to new replies.

Advertisement