Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Generation of random number using random class

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Post Generation of random number using random class

    This code uses the Random class to generate a random number up to 100.

    You can change the 100 value to any interger you wish.

    import java.util.*;
     
    public class RandomNumber {
     
        public static void main(String[] args) {
     
            Random random = new Random();
            int rand = random.nextInt(100);
     
            System.out.println(rand);
     
        }
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers


  2. #2
    Member Emperor_Xyn's Avatar
    Join Date
    Dec 2011
    Posts
    66
    My Mood
    Devilish
    Thanks
    21
    Thanked 2 Times in 2 Posts

    Default Re: How to Generate a random number using the Random class

    PHP Code:
     To change this templatechoose Tools Templates
     
    * and open the template in the editor.
     */
    package unogame;
    import java.util.*;
    import java.lang.*;
    import java.awt.*;
            
    /**
     *
     * @author Xyn
     */
    public class UNOGame {
    static 
    int ar;
     
    int counter=0;
    static 
    int winCount0;
    /**
         * @param args the command line arguments
         */

        
    public static void main(String[] args) {
         
    System.out.println("Welcome to Under or Over game.  Your opponnents number will be displayed and you have to go over or under it.");
         
      for(
    int i=1i<6i++)  {
         
    double cop = new Random() .nextInt(100) +1;
         
    double plp = new Random() .nextInt(100) +1;
         
    System.out.println(" The computers number is " cop " .")  ;
         
         
    Scanner keyboard = new Scanner(System.in);
         
    String pick keyboard.next();
          
    unofile uno = new unofile();  
            
            if (
    pick.equals("over")) {
             if (
    cop>plp) { 
                 
    System.out.println("You win that round!");
                
                 
    winCount++; 
             }}
         if (
    pick.equals("under")) {
             if (
    cop<plp) { 
                 
    System.out.println("You lose that round!");
                   
             }}
         if (
    pick.equals("over")) {
             if (
    cop<plp) { 
                 
    System.out.println("You win that round!");
               
                 
    winCount++; 
             }}
         if (
    pick.equals("under")) {
             if (
    cop>plp) { 
                 
    System.out.println("You lose that round!");
               
         
             }}
             
             
    System.out.print("Your number was ");
             
    System.out.print(plp "." );
                 
             }
          
    unofile uno = new unofile();
      
    uno.counterWin winCount;
     
    uno.writeWall();   
    }   
             

         
         



    Theres a sample of it in use. ((NOTE: POORLY CONSTRUCTED PROGRAM)

Similar Threads

  1. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 05:36 AM
  2. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 04:59 PM
  3. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 02:10 PM

Tags for this Thread