Cornelius Eanes's Programming Portal
Assignments (Random Numbers)
[53] Randomness

Goal: Learn how to generate random numbers in Java.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Randomness
/// File Name: Randomness.java
/// Date Finished: Oct 26, 2015

import java.util.Random;

public class Randomness {

    public static void main(String[] args) {
        // Random has a constructor that accepts an integer. This is called a seed, and will
        // generate the same sequence of "random numbers" every time. Passing no parameters
        // will instead use a random seed.
        Random r = new Random();

        int x = 1 + r.nextInt(10);

        System.out.println("My random number is " + x);

        // The parameter passed in the Random#nextInt(int) method determines the upward bounds
        // of the range of numbers it can generate. So passing in 5 would have the range be [0,4].
        System.out.println("Here are some numbers from 3 to 7!");
        System.out.print(3 + r.nextInt(5) + " ");
        System.out.print(3 + r.nextInt(5) + " ");
        System.out.print(3 + r.nextInt(5) + " ");
        System.out.print(3 + r.nextInt(5) + " ");
        System.out.print(3 + r.nextInt(5) + " ");
        System.out.print(3 + r.nextInt(5) + " ");
        System.out.println();

        System.out.println("Here are some numbers from 1 to 100!");
        System.out.print(1 + r.nextInt(100) + "\t");
        System.out.print(1 + r.nextInt(100) + "\t");
        System.out.print(1 + r.nextInt(100) + "\t");
        System.out.print(1 + r.nextInt(100) + "\t");
        System.out.print(1 + r.nextInt(100) + "\t");
        System.out.print(1 + r.nextInt(100) + "\t");
        System.out.println();

        // In this example, the range generated will be [0,9], but because of the addition of a
        // one, the range will now be [1,10].
        int num1 = 1 + r.nextInt(10);
        int num2 = 1 + r.nextInt(10);

        // Has a 1/10 chance of being true.
        if (num1 == num2)
        {
            System.out.println("The random numbers were the same! Weird.");
        }
        if (num1 != num2)
        {
            System.out.println("The random numbers were different! Not too surprising, actually.");
        }
    }
}

Output

[54] Magic 8 Ball

Goal: Create a fully-functioning virtual magic 8 ball.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Magic 8 Ball
/// File Name: MagicEightBall.java
/// Date Finished: Oct 26, 2015

import java.util.Random;

public class MagicEightBall {

    public static void main(String[] args) {

        Random rand = new Random();
        String response;
        int choice = rand.nextInt(20);

        if (choice == 0) {
            response = "It is certain";
        } else if (choice == 1) {
            response = "It is decidedly so";
        } else if (choice == 2) {
            response = "Without a doubt";
        } else if (choice == 3) {
            response = "Yes - definitely";
        } else if (choice == 4) {
            response = "You may rely on it";
        } else if (choice == 5) {
            response = "As I see it, yes";
        } else if (choice == 6) {
            response = "Most likely";
        } else if (choice == 7) {
            response = "Outlook good";
        } else if (choice == 8) {
            response = "Signs point to yes";
        } else if (choice == 9) {
            response = "Yes";
        } else if (choice == 10) {
            response = "Reply hazy, try again";
        } else if (choice == 11) {
            response = "Ask again later";
        } else if (choice == 12) {
            response = "Better not tell you now";
        } else if (choice == 13) {
            response = "Cannot predict now";
        } else if (choice == 14) {
            response = "Concentrate and ask again";
        } else if (choice == 15) {
            response = "Don't count on it";
        } else if (choice == 16) {
            response = "My reply is no";
        } else if (choice == 17) {
            response = "My sources say no";
        } else if (choice == 18) {
            response = "Outlook not so good";
        } else if (choice == 19) {
            response = "Very doubtful";
        } else {
            response = "You've broken the universe. All is lost.";
        }

        System.out.println("MAGIC 8 BALL SAYS: " + response);

    }

}

Output

[55] A Number-Guessing Game

Goal: Create a number-guessing game with the correct number being random.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Number Guessing Game
/// File Name: NumberGuessingGame.java
/// Date Finished: Oct 26, 2015

import java.util.Random;
import java.util.Scanner;

public class NumberGuessingGame {

    public static void main(String[] args) {

        Random rand = new Random();
        Scanner input = new Scanner(System.in);
        int correct = rand.nextInt(10) + 1, guess;

        System.out.println("I'm thinking of a number from 1 to 10.");
        System.out.print("Your guess: ");
        guess = input.nextInt();

        System.out.println();
        if (correct == guess) {
            System.out.println("That's right! My secret number was " + correct + ".");
        } else {
            System.out.println("Sorry, but I was really thinking of " + correct + ".");
        }

    }

}

Output

[56] Fortune Cookie

Goal: Generate a fortune cookie with a random fortune and random lucky numbers.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Fortune Cookie
/// File Name: FortuneCookie.java
/// Date Finished: Oct 26, 2015

import java.util.Random;

public class FortuneCookie {

    public static void main(String[] args) {

        Random rand = new Random();
        String fortune = "";
        int randNum = rand.nextInt(6);

        if (randNum == 0) {
            fortune = "Respect will come to you when you respect others";
        } else if (randNum == 1) {
            fortune = "You will go somewhere new soon";
        } else if (randNum == 2) {
            fortune = "Stick close to your friends";
        } else if (randNum == 3) {
            fortune = "Help! I'm held hostage at Wong's Fortune Cookie Factory!";
        } else if (randNum == 4) {
            fortune = "A random act of kindness will repay itself";
        } else if (randNum == 5) {
            fortune = "An exciting opportunity will present itself";
        }

        System.out.println("Fortune cookie says: " + fortune);
        System.out.println("\t" +
                (1 + rand.nextInt(54)) + " - " +
                (1 + rand.nextInt(54)) + " - " +
                (1 + rand.nextInt(54)) + " - " +
                (1 + rand.nextInt(54)) + " - " +
                (1 + rand.nextInt(54)) + " - " +
                (1 + rand.nextInt(54))
        );

    }

}

Output

[57] Dice

Goal: Roll two virtual dice and get their sum.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Dice
/// File Name: Dice.java
/// Date Finished: Oct 26, 2015

import java.util.Random;

public class Dice {

    public static void main(String[] args) {

        Random rand = new Random();

        int i1 = rand.nextInt(6) + 1;
        int i2 = rand.nextInt(6) + 1;

        System.out.println("HERE COMES THE DICE!");
        System.out.println();
        System.out.println("Roll #1: " + i1);
        System.out.println("Roll #2: " + i2);
        System.out.println("The total is " + (i1 + i2) + "!");

    }

}

Output

[58] One Shot Hi-Lo

Goal: Another number guessing game with output showing in you were too high or low.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: One Shot Hi-Lo
/// File Name: OneShotHiLo.java
/// Date Finished: Nov 09, 2015

import java.util.Random;
import java.util.Scanner;

public class OneShotHiLo {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Random rand = new Random();
        int randNumber = 1 + rand.nextInt(100), guess;

        System.out.print("I'm thinking of a number between 1-100. Guess what it is.\n> ");
        guess = input.nextInt();

        if (guess > randNumber) {
            System.out.println("Sorry, you are too high. I was thinking of " + randNumber + ".");
        } else if (guess < randNumber) {
            System.out.println("Sorry, you are too low. I was thinking of " + randNumber + ".");
        } else {
            System.out.println("You guessed it! It was " + randNumber + ".");
        }

    }

}

Output

[59] Three Card Monte

Goal: Create game that has a Three Card Monte situation.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Three-Card Monte
/// File Name: ThreeCardMonte.java
/// Date Finished: Nov 09, 2015

import java.util.Random;
import java.util.Scanner;

public class ThreeCardMonte {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        Random rand = new Random();
        int correct = 1 + rand.nextInt(3), guess;

        System.out.println("Three cups are placed in front of you. One of them has a ball in it.");
        System.out.println();
        System.out.println("Which one has the ball?");
        System.out.println();
        System.out.println("   ##       ##       ##");
        System.out.println("  ####     ####     ####");
        System.out.println("  ####     ####     ####");
        System.out.println("   1        2        3");
        System.out.println();
        System.out.print("> ");
        guess = input.nextInt();
        System.out.println();

        if (guess == correct) {
            System.out.println("You are correct! The ball was under cup #" + correct + ".");
        } else {
            System.out.println("You are incorrect! The was under cup #" + correct + ".");
        }
        System.out.println();
        System.out.println("   ##       ##       ##");
        if (correct == 1) {
            System.out.println("  #@@#     ####     ####");
            System.out.println("  #@@#     ####     ####");
        } else if (correct == 2) {
            System.out.println("  ####     #@@#     ####");
            System.out.println("  ####     #@@#     ####");
        } else {
            System.out.println("  ####     ####     #@@#");
            System.out.println("  ####     ####     #@@#");
        }
        System.out.println("   1        2        3");


    }

}

Output