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 1 of 1

Thread: my program. Random number generater.

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default my program. Random number generater.

    my program first says:

    As we know if you could only pick a number
    out of 1 million your chances are 1 in 1 million:
    today I am going to prove this...I hope

    you can pick a number from 2 to 10000000000000000000 ish <-- this is the number Range
    then you pick a number you want to come up
    and how many times you wish for it to occur
    password is abc123... incase you can't find it

    bug free i hope

    /* made by James Anderson
     *version 0.1.6 alpha
     *V 0.1.6 can occurs a number of times
     *V 0.1.5 changed name from lotto3 to RandomNumbers.java 
     *V 0.1.4 (added password for fun)
     */
     import java.util.Scanner;
     import java.util.Random;
     
    public class RandomNumbers {
    	public static void main (String[] args) {
    		Scanner myScanner = new Scanner(System.in);
    		Random myRandom = new Random();
    		int RandomNumber = 0, UserNumber = 0, givennumber=0, TotalNumbers = 0, NumberOfTimes=0,InputNumberOfTimes = 0;
    		String Password = "abc123", userinput;
     
    	System.out.println ("version 0.1.5");
    //password
     
    		System.out.print ("please type in the password:	");
    		userinput = myScanner.nextLine();
    		if (Password.equalsIgnoreCase(userinput)){
    			System.out.println ("Right Password.");
    			System.out.println ();
     
     
     
    //explan to user
     
    		System.out.println ("As we know if you could only pick a number");
    		System.out.println ("out of 1 million your chances are 1 in 1 million:");
    		System.out.println ("today I am going to prove this...I hope");
    		System.out.println ();
     
    //totalnumber setup	
     
    		do{
    		System.out.print("Pick a maximum number (larger numbers take longer): ");
    		System.out.println ("must be greater than 1");
    		TotalNumbers = myScanner.nextInt();
    		}while(TotalNumbers < 2);
     
    //example of a number and explaning to pick a number
     
    		givennumber = (myRandom.nextInt(TotalNumbers) + 1);
     
    		System.out.println("Pick a number between 1 and " + Integer.toString(TotalNumbers) + " (for example " + Integer.toString(givennumber) + "): ");
    		System.out.println("Bad responses will be forced to reenter:");
     
     
    //user picks a number				
    		do{
    		UserNumber = myScanner.nextInt();
    		if (UserNumber > TotalNumbers) {
    			System.out.println ("Must be less than " + TotalNumbers + " Please type again:");
    		}
    		else if (UserNumber < 1){
    			System.out.println ("must be greater than 0 Please type again:" );
    		}
    		}while(UserNumber < 1 || UserNumber > TotalNumbers);
     
    // number of times
    		System.out.println ("how many times would you like it to occur?");
    		InputNumberOfTimes = myScanner.nextInt();
     
    //run
    		System.out.println ();
    		System.out.println ();
    		for (int times=0; (RandomNumber != UserNumber);) {
    		do {
    //reset
    			RandomNumber = -1;
    		for (; (RandomNumber != UserNumber); times ++){
    			RandomNumber = (myRandom.nextInt(TotalNumbers) + 1);
    			System.out.println (RandomNumber);
     
     
    		}
    		NumberOfTimes++;
    			} while (NumberOfTimes != InputNumberOfTimes);
    //finish	 
    			 System.out.println ();
    			 System.out.print ("It took ");
    			 System.out.print (times);
    			 System.out.println (" times until your number came up; "+ NumberOfTimes +" times.");
    			 System.out.println ("With: " + (TotalNumbers) + " likely numbers that could pop up (this is not fake)");
    			 System.out.println ("Can you believe it...want to try again?");
     
    		}
    		}
    		else {
    			System.out.println ("Sorry Wrong password");
    		}
    		}
     
    	}
    Last edited by james137; December 2nd, 2009 at 02:04 PM.


Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 04:46 PM
  2. Help With Odd/Even number program
    By JonoScho in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 23rd, 2009, 09:53 AM
  3. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2009, 10:44 PM
  4. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM