Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java: Random number generator?

Options
  • 08-04-2002 7:40pm
    #1
    Registered Users Posts: 631 ✭✭✭


    Could any one please help me, I know this may seem trivial to some but as I have only started studying java this year, I am finding this difficult!

    I want to create an applet which will have a button that when pressed will generate a random* number between 1 and 500!

    I know that there is a utility for this as i have seem it incorperated in a dice roll program!

    Thanks in advance!
    Willie



    p.s. i have scoured the Internet already! :(


    * random = to system clock will do (thats how the utility works)


Comments

  • Registered Users Posts: 1,176 ✭✭✭podgeen


    take a look at
    java.util.Random
    if you have any questions relating to using this class, I will be happy to help. (others too im sure)


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    This'll do it:
    int randNumber=(int)(Math.random()*499.0)+1;
    
    or else you can use the Random class like Podgeen said - seed it with the system time:
    Random r=new Random(System.currentTimeMillis());
    int randNumber=r.nextInt(500)+1;
    


  • Registered Users Posts: 631 ✭✭✭FatB


    Originally posted by podgeen
    take a look at
    java.util.Random
    Originally posted by FatB
    I know that there is a utility for this as i have seem it incorperated in a dice roll program!

    Thats what i meant, I have tried to make it work, it just wont work! I'll post my code in about an hour when i get to the computer i have it stored on!


    Thanks...
    FatB


  • Closed Accounts Posts: 852 ✭✭✭m1ke


    for (int i=0; i<500; i++)
    int n = (int)(Math.random()*1000);

    somethin like that should do it


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    umm..... eh?


  • Advertisement
Advertisement