> ## Documentation Index
> Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Random Numbers

> 8 assignments

## 41. Randomness - [Link](https://programmingbydoing.com/a/randomness.html)

<Tabs>
  <Tab title="Task">
    You know what's cool? Having the computer randomly choose a number. This is the basis of pretty much every computer game ever.

    To pick a random number, you first need to `import java.util.Random;`.

    Then, you must create a random-number generator object, like so:

    ```java theme={null}
    Random r = new Random();
    ```

    Once that's finished, you can have the computer pick a random integer like this:

    ```java theme={null}
    int x = 1 + r.nextInt(10);
    ```

    That'll pick a random number from 1 to 10 (inclusive) and store it into the variable x. Enough of the explaining; let's look at some code!

    **Files Needed :**

    * [Randomness.java](https://programmingbydoing.com/a/examples/Randomness.java)

    **Sample Output :**

    Your random numbers will probably be different than these. Actually, that's kind of the point.

    ```bash theme={null}
    My random number is 8
    Here are some numbers from 1 to 5!
    1 1 5 4 2 2
    Here are some numbers from 1 to 100!
    25      25      39      34      93      13
    The random numbers were different! Not too surprising, actually.
    ```
  </Tab>

  <Tab title="Answer">
    ```java Randomness.java theme={null}
    import java.util.Random;

    public class Randomness
    {
        public static void main ( String[] args )
        {
            Random r = new Random();

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

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

            System.out.println( "Here are some numbers from 1 to 5!" );
            System.out.print( 1 + r.nextInt(5) + " " );
            System.out.print( 1 + r.nextInt(5) + " " );
            System.out.print( 1 + r.nextInt(5) + " " );
            System.out.print( 1 + r.nextInt(5) + " " );
            System.out.print( 1 + r.nextInt(5) + " " );
            System.out.print( 1 + 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();

            int num1 = 1 + r.nextInt(10);
            int num2 = 1 + r.nextInt(10);

            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." );
            }
        }
    }
    ```
  </Tab>

  <Tab title="Question">
    Assignments turned in *without* these things will not receive any points.

    1. Delete the `1 +` in front of all six lines that pick numbers 1-5, so that they look like this: `System.out.print( r.nextInt(5) + " " );` Run the program a few times, and see if you can figure out what range the new random numbers are in.
       > Answer: 0-4
    2. Change the `1 +` in front of all six lines that pick numbers 1-5, so that they look like this: `System.out.print( 3 + r.nextInt(5) + " " );` Run the program a few times. Is it picking random numbers from 3 to 5? If not, what range are they?
       > Answer: 3-7
    3. Change the line where you create the random number generator so that it looks like this: `Random r = new Random(12353);` This number is called a seed. Run the program a few times. What do you notice? What happened to the random numbers?
       > Answer: The random numbers are the same
    4. Change to random seed to something else and observe the behavior. What happens to the random numbers?
       > Answer: The random numbers are different
    5. (Delete the random seed before turning in the assignment.)
       > Answer: Done
  </Tab>
</Tabs>

## 42. Magic 8-Ball - [Link](https://programmingbydoing.com/a/magic-8ball.html)

<Tabs>
  <Tab title="Task">
    **Files Needed :**

    * [Magic8Ball.java](https://programmingbydoing.com/a/examples/Magic8Ball.java)

    **Sample Output :**

    ```bash theme={null}
    MAGIC 8-BALL SAYS: It is decidedly so
    ```

    ```bash theme={null}
    MAGIC 8-BALL SAYS: Reply hazy, try again
    ```

    ```bash theme={null}
    MAGIC 8-BALL SAYS: Signs point to yes
    ```
  </Tab>

  <Tab title="Answer">
    ```java Magic8Ball.java theme={null}
    import java.util.Random;

    public class Magic8Ball
    {
        public static void main ( String[] args )
        {
            Random r = new Random();

            int choice = 1 + r.nextInt(15);
            String response = "";

            if ( choice == 1 )
                response = "It is certain";
            else if ( choice == 2 )
                response = "It is decidedly so";
            else if ( choice == 3 )
                response = "Without a doubt";
            else if ( choice == 4 )
                response = "Yes - definitely";
            else if ( choice == 5 )
                response = "You may rely on it";
            else if ( choice == 6 )
                response = "As I see it, yes";
            else if ( choice == 7 )
                response = "Most likely";
            else if ( choice == 8 )
                response = "Outlook good";
            else if ( choice == 9 )
                response = "Signs point to yes";
            else if ( choice == 10 )
                response = "Yes";
            else if ( choice == 11 )
                response = "Reply hazy, try again";
            else if ( choice == 12 )
                response = "Ask again later";
            else if ( choice == 13 )
                response = "Better not tell you now";
            else if ( choice == 14 )
                response = "Cannot predict now";
            else if ( choice == 15 )
                response = "Concentrate and ask again";
            else
                response = "8-BALL ERROR!";

            System.out.println( "MAGIC 8-BALL SAYS: " + response );
        }
    }
    ```
  </Tab>

  <Tab title="Question">
    Assignments turned in *without* these things will not receive any points.

    1. The real Magic 8-Ball™ contains 20 responses, not 15. Change the code so that it picks a random number from 1-20, and add the following five responses:
       * "Don't count on it"
       * "My reply is no"
       * "My sources say no"
       * "Outlook not so good"
       * "Very doubtful"
       > Answer:
       ```java theme={null}
       int choice = 1 + r.nextInt(20);
       ...
       else if ( choice == 16 )
           response = "Don't count on it";
       else if ( choice == 17 )
           response = "My reply is no";
       else if ( choice == 18 )
           response = "My sources say no";
       else if ( choice == 19 )
           response = "Outlook not so good";
       else if ( choice == 20 )
           response = "Very doubtful";
       ```
  </Tab>
</Tabs>

## 43. A Number-Guessing Game - [Link](https://programmingbydoing.com/a/a-number-guessing-game.html)

<Tabs>
  <Tab title="Task">
    Modify your [incredibly](https://programmingbydoing.com/a/the-worst-number-guessing-game-ever.html) stupid number-guessing game to actually pick a random number from 1 to 10 and have the user try to guess that. Tell them if they get it right or wrong, and if they get it wrong, show them what the random number was.

    They will still only get one try.
    **Sample Output :**

    ```bash theme={null}
    I'm thinking of a number from 1 to 10.
    Your guess: 3

    Sorry, but I was really thinking of 4.
    ```

    ```bash theme={null}
    I'm thinking of a number from 1 to 10.
    Your guess: 4

    Sorry, but I was really thinking of 7.
    ```

    ```bash theme={null}
    I'm thinking of a number from 1 to 10.
    Your guess: 2

    That's right!  My secret number was 2!
    ```
  </Tab>

  <Tab title="Answer">
    ```java NumberGuess.java theme={null}
    import java.util.Random;
    import java.util.Scanner;

    public class NumberGuess
    {
        public static void main ( String[] args )
        {
            Random r = new Random();
            Scanner keyboard = new Scanner(System.in);

            int secret = 1 + r.nextInt(10);
            int guess;

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

            System.out.println();

            if ( guess == secret )
                System.out.printf( "That's right!  My secret number was %d!\n", secret );
            else
                System.out.printf( "Sorry, but I was really thinking of %d.\n", secret );
        }
    }
    ```
  </Tab>
</Tabs>

## 44. Fortune Cookie - [Link](https://programmingbydoing.com/a/fortune-cookie.html)

<Tabs>
  <Tab title="Task">
    Write a program that simulates a random fortune from a fortune cookie. You must have at least six fortunes.

    For bonus points, also add randomly-chosen lotto numbers to the fortune. In Texas, the lotto chooses 6 numbers, each from 1-54.

    ```bash theme={null}
    Fortune cookie says: "You will find happiness with a new love."
    ```

    ```bash theme={null}
    Fortune cookie says: "Stick with your wife."
        13 - 44 - 19 - 37 - 29 - 17
    ```
  </Tab>

  <Tab title="Answer">
    ```java FortuneCookie.java theme={null}
    import java.util.Random;

    public class FortuneCookie
    {
        public static void main ( String[] args )
        {
            Random r = new Random();
            int choice = 1 + r.nextInt(6);
            String response = "";

            if ( choice == 1 )
                response = "You will find happiness with a new love.";
            else if ( choice == 2 )
                response = "Stick with your wife.";
            else if ( choice == 3 )
                response = "You will find happiness with a new love.";
            else if ( choice == 4 )
                response = "Stick with your wife.";
            else if ( choice == 5 )
                response = "You will find happiness with a new love.";
            else if ( choice == 6 )
                response = "Stick with your wife.";

            System.out.println( "Fortune cookie says: " + response );

            System.out.print( "\t" );
            System.out.print( 1 + r.nextInt(54) + " - " );
            System.out.print( 1 + r.nextInt(54) + " - " );
            System.out.print( 1 + r.nextInt(54) + " - " );
            System.out.print( 1 + r.nextInt(54) + " - " );
            System.out.print( 1 + r.nextInt(54) + " - " );
            System.out.print( 1 + r.nextInt(54) );
        }
    }
    ```
  </Tab>
</Tabs>

## 45. Dice - [Link](https://programmingbydoing.com/a/dice.html)

<Tabs>
  <Tab title="Task">
    Write a program that simulates a dice roll by picking a random number from 1-6 and then picking a second random number from 1-6. Add the two values together, and display the total.

    **Sample Output :**

    ```bash theme={null}
    HERE COMES THE DICE!

    Roll #1: 3
    Roll #2: 5
    The total is 8!
    ```

    ```bash theme={null}
    HERE COMES THE DICE!

    Roll #1: 4
    Roll #2: 2
    The total is 6!
    ```
  </Tab>

  <Tab title="Answer">
    ```java Dice.java theme={null}
    import java.util.Random;

    public class Dice
    {
        public static void main ( String[] args )
        {
            Random r = new Random();
            int choice1 = 1 + r.nextInt(6);
            int choice2 = 1 + r.nextInt(6);

            System.out.println( "HERE COMES THE DICE!" );

            System.out.printf( "\nRoll #1: %d", choice1 );
            System.out.printf( "\nRoll #2: %d", choice2 );
            System.out.printf( "\nThe total is %d!", choice1 + choice2 );
        }
    }
    ```
  </Tab>
</Tabs>

## 46. One Shot Hi-Lo - [Link](https://programmingbydoing.com/a/one-shot-hi-lo.html)

<Tabs>
  <Tab title="Task">
    Write a program that picks a random number from 1-100. Give the user a chance to guess it. If they get it right, tell them so. If their guess is higher than the number, say "Too high." If their guess is lower than the number, say "Too low." Then quit. (They don't get any more guesses for now.)

    **Sample Output :**

    ```bash theme={null}
    I'm thinking of a number between 1-100.  Try to guess it.
    > 13

    Sorry, you are too low.  I was thinking of 34.
    ```

    ```bash theme={null}
    I'm thinking of a number between 1-100.  Try to guess it.
    > 79

    Sorry, you are too high.  I was thinking of 51.
    ```

    ```bash theme={null}
    I'm thinking of a number between 1-100.  Try to guess it.
    > 42

    You guessed it!  What are the odds?!?
    ```
  </Tab>

  <Tab title="Answer">
    ```java OneShotHiLo.java theme={null}
    import java.util.Random;
    import java.util.Scanner;

    public class OneShotHiLo
    {
        public static void main ( String[] args )
        {
            Random r = new Random();
            Scanner keyboard = new Scanner(System.in);

            int secret = 1 + r.nextInt(100);
            int guess;

            System.out.println( "I'm thinking of a number between 1-100.  Try to guess it." );
            System.out.print( "> " );
            guess = keyboard.nextInt();

            System.out.println();

            if ( guess == secret )
                System.out.printf( "You guessed it!  What are the odds?!?\n" );
            else if ( guess < secret )
                System.out.printf( "Sorry, you are too low.  I was thinking of %d.\n", secret );
            else if ( guess > secret )
                System.out.printf( "Sorry, you are too high.  I was thinking of %d.\n", secret );
        }
    }
    ```
  </Tab>
</Tabs>

## 47. Three Card Monte - [Link](https://programmingbydoing.com/a/three-card-monte.html)

<Tabs>
  <Tab title="Task">
    Write a program that allows the user to play a simple game of Three Card Monte. Create three cards. One of the cards is the Queen. The others are Jokers. Shuffle the cards, then ask the player to pick a card. After the player picks a card, shuffle the cards again and show the player that the Queen is not where they picked. If the player picks the Queen, they win. If they don't pick the Queen, they lose.

    **Sample Output :**

    ```bash theme={null}
    You slide up to Fast Eddie's card table and plop down your cash.
    He glances at you out of the corner of his eye and starts shuffling.
    He lays down three cards.

    Which one is the ace?

        ##  ##  ##
        ##  ##  ##
        1   2   3

    > 2

    Ha! Fast Eddie wins again! The ace was card number 3.

        ##  ##  AA
        ##  ##  AA
        1   2   3

    ```

    (Note that this is basically just a number-guessing game with fancy graphics.)

    ```bash theme={null}
    You slide up to Fast Eddie's card table and plop down your cash.
    He glances at you out of the corner of his eye and starts shuffling.
    He lays down three cards.

    Which one is the ace?

        ##  ##  ##
        ##  ##  ##
        1   2   3

    > 2

    You nailed it! Fast Eddie reluctantly hands over your winnings, scowling.

        ##  AA  ##
        ##  AA  ##
        1   2   3
    ```
  </Tab>

  <Tab title="Answer">
    ```java ThreeCardMonte.java theme={null}
    import java.util.Random;
    import java.util.Scanner;

    public class ThreeCardMonte
    {
        public static void main ( String[] args )
        {
            Random r = new Random();
            Scanner keyboard = new Scanner(System.in);

            int ace = 1 + r.nextInt(3);

            System.out.println( "You slide up to Fast Eddie's card table and plop down your cash." );
            System.out.println( "He glances at you out of the corner of his eye and starts shuffling." );
            System.out.println( "He lays down three cards." );

            System.out.println();
            System.out.println( "Which one is the ace?" );
            System.out.println();
            System.out.println( "\t##  ##  ##" );
            System.out.println( "\t##  ##  ##" );
            System.out.println( "\t1   2   3" );

            System.out.print( "\n> " );
            int guess = keyboard.nextInt();

            System.out.println();

            if ( guess == ace )
                System.out.println( "You nailed it! Fast Eddie reluctantly hands over your winnings, scowling." );
            else
                System.out.printf( "Ha! Fast Eddie wins again! The ace was card number %d.\n", ace );

            System.out.println();
            if ( ace == 1 ) {
                System.out.println( "\tAA ## ##" );
                System.out.println( "\tAA ## ##" );
                System.out.println( "\t1  2  3" );
            } else if ( ace == 2 ) {
                System.out.println( "\t## AA ##" );
                System.out.println( "\t## AA ##" );
                System.out.println( "\t1  2  3" );
            } else {
                System.out.println( "\t## ## AA" );
                System.out.println( "\t## ## AA" );
                System.out.println( "\t1  2  3" );
            }
        }
    }
    ```
  </Tab>
</Tabs>

## 75. Baby Blackjack - [Link](https://programmingbydoing.com/a/baby-blackjack.html)

<Tabs>
  <Tab title="Task">
    Write a program that allows a human user to play a single hand of "blackjack" against a dealer.

    1. Pick two values from 1-10 for the player. These are the player's "cards".
    2. Pick two more values from 1-10 for the dealer.
    3. Whoever has the highest total is the winner.
    4. There is no betting, no busting, and no hitting. Save that for real blackjack.

    **Sample Output :**

    ```bash theme={null}
    Baby Blackjack!

    You drew 6 and 5.
    Your total is 11.

    The dealer has 7 and 3.
    Dealer's total is 10.

    YOU WIN!
    ```
  </Tab>

  <Tab title="Answer">
    ```java BabyBlackjack.java theme={null}
    import java.util.Random;

    public class BabyBlackjack
    {
        public static void main ( String[] args )
        {
            Random r = new Random();

            int player1 = 1 + r.nextInt(10);
            int player2 = 1 + r.nextInt(10);
            int dealer1 = 1 + r.nextInt(10);
            int dealer2 = 1 + r.nextInt(10);

            int playerTotal = player1 + player2;
            int dealerTotal = dealer1 + dealer2;

            System.out.println( "Baby Blackjack!\n" );

            System.out.printf( "You drew %d and %d.\n", player1, player2 );
            System.out.printf( "Your total is %d.\n\n", playerTotal );

            System.out.printf( "The dealer has %d and %d.\n", dealer1, dealer2 );
            System.out.printf( "Dealer's total is %d.\n\n", dealerTotal );

            if ( playerTotal > dealerTotal )
                System.out.println( "YOU WIN!" );
            else
                System.out.println( "DEALER WINS!" );
        }
    }
    ```
  </Tab>
</Tabs>
