Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by lupine Nov 7, 2000 at 1:56 PM 3 replies 500+ views
Original Post
lupine
lupine
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
loserkid
loserkid
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
lupine
lupine
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
loserkid
loserkid
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
deakin
deakin
There is another method you can use like this:

  
// create new random object and seed it with time

Random random = new Random(System.currentTimeMillis());

// Returns a random number in the given range

private int getRandomNum(int range){
return Math.abs(random.nextInt() % range);
}


- DanielMy homepage

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.