Tag Archives: rand

Python || Random Number Guessing Game Using Random MyRNG & While Loop

Here is another homework assignment which was presented in introduction class. The following is a simple guessing game using commandline arguments, which demonstrates the use of generating random numbers.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

How To Get User Input
Getting Commandline Arguments
While Loops
MyRNG.py - Random Number Class

==== 1. DESCRIPTION ====

The following program is a simple guessing game which demonstrates how to generate random numbers using python. This program will seed the random number generator (located in the file MyRNG.py), select a number at random, and then ask the user for a guess. Using a while loop, the user will keep attempting to guess the selected random number until the correct guess is obtained, afterwhich the user will have the option of continuing play or exiting.

==== 2. USAGE ====

The user enters various options into the program via the commandline. An example of how the commandline can be used is given below.


python3 guess.py [-h] [-v] -s [seed] -m 2 -M 353

Where the brackets are meant to represent features which are optional, meaning the user does not have to specify them at run time.

The -m and -M options are mandatory.

• -M is best picked as a large prime integer
• -m is best picked as an integer in the range of 2,3,..,M-1

NOTE: The use of commandline arguments is not mandatory. If any of the mandatory options are not selected, the program uses its own logic to generate random numbers.

==== 3. FEATURES ====

The following lists and explains the command line argument options.

-s (seed): Seed takes an integer as a parameter and is used to seed the random number generator. When omitted, the program uses its own logic to seed the generator

-v (verbose): Turn on debugging messages.

-h (help): Print out a help message which tells the user how to run the program and a brief description of the program.

-m (minimum): Set the minimum of the range of numbers the program will select its numbers from.

-M (maximum): Set the maximum of the range of numbers the program will select its numbers from.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:

Seed = 806189064, Minimum = 1, Maximum = 1000

I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.
>> 500

Sorry that was not correct, please try again...

>> 400

WARMER

>> 600

COLDER

>> 300

WARMER

>> 150

WARMER

>> 100

COLDER

>> 180

COLDER

>> 190

COLDER

>> 130

WARMER

>> 128

WINNER! You have guessed correctly!
It took you 10 attempt(s) to find the answer!

Would you like to play again? (Yes or No): y

------------------------------------------------------------

Make a guess between 1 and 1000

>> 500

Sorry that was not correct, please try again...

>> 600

COLDER

>> 400

WARMER

>> 300

WARMER

>> 280

WARMER

>> 260

WARMER

>> 250

COLDER

>> 256

WINNER! You have guessed correctly!
It took you 8 attempt(s) to find the answer!

Would you like to play again? (Yes or No): n

Thanks for playing!!

C++ || Random Number Guessing Game Using Srand, Rand, & Do/While Loop

This is a simple guessing game, which demonstrates the use of srand and rand to generate random numbers. This program first prompts the user to enter a number between 1 and 1000. Using if/else statements, the program will then check to see if the user inputted number is higher/lower than the pre defined random number which is generated by the program. If the user makes a wrong guess, the program will re prompt the user to enter in a new number, where they will have a chance to enter in a new guess. Once the user finally guesses the correct answer, using a do/while loop, the program will ask if they want to play again. If the user selects yes, the game will start over, and a new random number will be generated. If the user selects no, the game will end.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

Srand
Rand
Modulus
Ctime
Do/While Loop

NOTE: On some compilers, you may have to add #include < cstdlib> in order for the code to compile.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output:

I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.

67
Too low! Think higher.
500
Too low! Think higher.
700
Too high! Think lower.
600
Too low! Think higher.
680
Too high! Think lower.
650
Too low! Think higher.
660
Too low! Think higher.
670
You got it, and it only took you 8 trys!
Would you like to play again (y/n)? y

------------------------------------------------------------

Make a guess (between 1-1000):

500
Too low! Think higher.
600
Too low! Think higher.
700
Too low! Think higher.
900
Too high! Think lower.
800
Too high! Think lower.
760
Too high! Think lower.
740
Too high! Think lower.
720
Too high! Think lower.
700
Too low! Think higher.
710
Too high! Think lower.
705
Too high! Think lower.
701
Too low! Think higher.
702
Too low! Think higher.
703
Too low! Think higher.
704
You got it, and it only took you 15 trys!
Would you like to play again (y/n)? n

Thanks for playing!!