Skip to main content
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:
Random r = new Random();
Once that’s finished, you can have the computer pick a random integer like this:
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 :Sample Output :Your random numbers will probably be different than these. Actually, that’s kind of the point.
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.
Files Needed :Sample Output :
MAGIC 8-BALL SAYS: It is decidedly so
MAGIC 8-BALL SAYS: Reply hazy, try again
MAGIC 8-BALL SAYS: Signs point to yes
Modify your incredibly 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 :
I'm thinking of a number from 1 to 10.
Your guess: 3

Sorry, but I was really thinking of 4.
I'm thinking of a number from 1 to 10.
Your guess: 4

Sorry, but I was really thinking of 7.
I'm thinking of a number from 1 to 10.
Your guess: 2

That's right!  My secret number was 2!
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.
Fortune cookie says: "You will find happiness with a new love."
Fortune cookie says: "Stick with your wife."
    13 - 44 - 19 - 37 - 29 - 17
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 :
HERE COMES THE DICE!

Roll #1: 3
Roll #2: 5
The total is 8!
HERE COMES THE DICE!

Roll #1: 4
Roll #2: 2
The total is 6!
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 :
I'm thinking of a number between 1-100.  Try to guess it.
> 13

Sorry, you are too low.  I was thinking of 34.
I'm thinking of a number between 1-100.  Try to guess it.
> 79

Sorry, you are too high.  I was thinking of 51.
I'm thinking of a number between 1-100.  Try to guess it.
> 42

You guessed it!  What are the odds?!?
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 :
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.)
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
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 :
Baby Blackjack!

You drew 6 and 5.
Your total is 11.

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

YOU WIN!