Beginners Guide to C++ Programming - Part 4

Even More Functions - Generating Random Numbers

Bulletbutter
In the last tutorial we talked briefly about functions. Since practice makes perfect we are going to review how to declare a function.

return type name (parameters)
{
statement;
}

The return type of a function is much like a variable type. If you can recall we talked about these in part 1. The return type for a function is basically what type of value we will be returning to main after the function is complete. Take this function for example:

int Add (int x, int y)
{
int sum;
sum=x+y;
return sum;
}
Return Type:
As you can see this basic function adds two variables (x and y) and returns the sum of them to the main function. These values are numbers, or integers, which is why we have the return type set as an int. All functions must have a return type in order for the compiler to run the code without giving you errors.

Function Name:
The function name can be anything you want it to be, so long as it follows the naming rules. In the above example I named the function for what it does, add.

Parameter:
In order for the function to be able to add anything it has to receive two variables. As you can see we used x and y to hold the values of the variables that were passed from main to the Add function.

Statement:
The statements we used was the actual declaration of a new variable (sum), and mathematical equation to add the two variables together (sum=x+y) and the return statement (return sum). The bulk of the statement should contain all the code that you want your function to execute.

Functions are the 'meat and potatoes' of C++ programming. I highly recommend you try setting up functions of your own and practice, practice, practice. Keep practicing until you can do it with your eyes closed.

Rand():
Continuing on with this tutorial we are going to go over a built in function. There are several built in functions that C++ has, for now we are just going to go over a rather important one, the random number generating function.

The rand() function will choose a random number between 0 and RAND_MAX. RAND_MAX is a reserved keyword for C++. It represents the maximum value that the random function can generate, 32767. In this tutorial we will not need to generate random numbers that high. I will show you how to control the random function to generate more realistic numbers. As with all function, we can set it up two different ways. We can create the function and make it a stand alone, all purpose function. Or we can create a variable in main and let main take care of outputting the random numbers. Since I have already shown you the latter, the next piece of code will be our random function with a little bit of flare. In the next bit of code you will see, we are going to simulate rolling dice. You will see some things that we have not yet talked about. I will go over them when I explain how the program works.

1.#include
2.#include //new header file!
3.
4.using namespace std;
5.
6.void rollDice(void);//function declaration, we will go over void
7.
8. int main();//start main
9. {
10. srand(time (0)); //makes the numbers random based on current time
11.
12. rollDice(); // function call for rollDice();
13.
14.return 0;
15.}
16..
17.void rollDice( void ) // begin function definition
18.{
19. int die1; //variable for first random #
20. int die2; //variable for second random #
21..
22. die1 = 1 + rand() % 6; //generate random number between 1-6
23. die2 = 1 + rand() % 6; //generate another number between 1-6
24. cout <<"The first dice is " < 25. cout <<"The second dice is "< 26.} //end function

On line 2 we are using a new include header file. The included header file ( ) allows us to use the code on line 10.This line works hand in hand with line 10. Line 10, srand(time(0), allows us to get different random numbers each time we run the program. If we didn't use these two lines and just used 1 + rand() % 6 on lines 22 and 23, in the rollDice() function, we would get the same random numbers each time we ran the program (which wouldn't make it appear very random).

On line 6 we are introduced to a new return type and a new parameter type, void. The void return type and parameter type basically means that the function will NOT return anything and it will not receive any values from the main() function. If you take a look at the rollDice() function, starting on line 17, you will see that it generates two random numbers and outputs them. We do not need to modify the random numbers in the main() function, therefore the values do not need to be returned to the main() function. The random numbers are actually output in the rollDice() function, not the main() function.

Line 12, rollDice(), is the function call. At this point the program leaves the main function and jumps down to line 17 and executes line 17-26. Once it has finished, the program returns to line 14 (which ends the main() function.

Lines 17-26 is the rollDice() function definition. Since we do not need any values from main, the parameters are of type void. And since we are not returning any values to the main() function, the return type is also void.

On lines 19 and 20 we set up two variables for the rollDice() function. The value of these two variables will be assigned to random numbers.

Lines 22 and 23 is where the random numbers are generated. This is typically how you use the rand() function. You first have to assign the random number generation function (rand()) to a variable. Then you have to tell the rand() function which numbers to choose from. When I first started talking about the rand() function, I stated that it will generate a random number between 0-32767. As you know, dice only have 6 sides and start with 1. So generating a random number between 0-32767 would not be appropriate for what we are trying to accomplish. To control which numbers rand() will produce we did the following…
1 + rand() % 6;
Place 1 + before rand() tells the random function to add 1 to the first number it is allowed to generate, which is 0. Placing % 6 after rand() tells the rand() function the highest number it can generate. So if you wanted to generate higher numbers you would just simply change the first value (1) and the last value (6). Practice generating different random numbers. Just keep in mind that you can not generate a higher number than RAND_MAX without modifying the code.
In the next tutorial we are going to go over the enum function.

Published by Bulletbutter

http://twitter.com/bulletbutter Learning has to be my most favorite thing to do. Computers come in a close second, mostly because that is the tool I use to do most of my learning. Trying to get my website...  View profile

  • Generating random numbers is used more for simulation and is an important function for making games.
  • Remember how to modify the rand() function and generate random numbers in a range that you need.
  • Functions are the 'meat and potatoes' of the C++ language. Lean to use them correctly.
Once you know how to declare variables, generate random numbers and how to use functions correctly you can start making simple games (like craps).

1 Comments

Post a Comment
  • seawoodareke4/24/2008

    in many and eat to my parents from just their Now, having by year.

To comment, please sign in to your Yahoo! account, or sign up for a new account.