- When you can't find an operator to do your math
you need to turn to the C library.
There you'll find fund and friendly functions
to sate your mathematical whims.
In this movie I cover two of the typical
C language math library functions,
the square root function and the power function.
I'll also introduce the random number generating
function, which isn't really a math function,
but it's something you may find quite handy.
To generate the square root of a value
you use the sqrt function.
It reads like squirt, but it's really square root.
The square root function is shown at line eight.
It requires the math.h header file
which is included on line two.
The function swallows a floating point value
specified as 2.0.
It returns the square root as a floating point value
which is saved in the float variable r.
The print f function on line 10 displays the results.
Build and run the code.
And now you know the square root of two
or any other value you specify in the code.
Another math function worthy of note
is the pow function which is short for power.
The C language lacks a power operator,
which is common in other programming languages,
but the pow function is far more versatile.
Open the pow file.
The pow function appears at line eight.
It also requires the math.h header file
as shown on line two.
The pow function uses two arguments,
both floating point values.
The first is the base, the second is the exponent.
So here two is being raised to the eighth power.
The result is saved in floating point variable p,
and then it's printed in line 10.
Build and run.
Two to the eighth power is 256.
Generally speaking, all C language math functions
require the inclusion of the math.h header file.
They all work with real numbers.
I use float in my examples, but officially
they should be double type variables.
The double carries twice the precision of a float.
That means it's more accurate, but it should
really be used only when a greater degree
of precision is required.
Another popular number function, although it's
not a true math function, is rand.
It generates pseudo-random numbers.
They're pseudo-random because they can
be predicted, but they're still good enough
to be random in a general sense.
The rand function requires the stdlib,
or standard library header file, for its definition.
This is the second most common header file
after stdio.
The function itself requires no arguments.
It simply generates a random integer value.
That value here is stored in variable r.
The print f function in line 10 displays the result.
Build and run.
On my screen I see 41 is the random number.
Run the code again.
And it's the same number.
This isn't a bug, it's predictable.
For the rand function to work best you must
seed the randomizer.
That's a software machine that helps
generate random numbers.
The random seeding function is called srand,
which is also defined in the stdlib header file.
In the code insert the srand function
in the line above rand.
The srand function requires a positive integer
argument to seed the randomizer.
Here I'm using the value 66, which I just
pulled out of my head.
Save the changes, build and run.
This time a new random number appears,
but will the same number appear
when you run the code again?
Let's find out.
Yep.
You have several tools available
to reseed the randomizer.
One is that you could ask the user to input
a positive integer value when the program starts,
and that's kind of lame.
What most programmers do is to borrow
the current tick-tock value
from the computer's internal time clock.
I cover time functions in a later movie.
For now we need to change line eight in the code
to replace the value 66 with a time function.
We need to type in parentheses the word unsigned,
then the time function time, and in its parentheses
you need to put the word NULL in all caps.
N-U-L-L.
Make sure all the parentheses match up
as they're highlighted on the screen.
Good.
Yes, this looks odd, but it's important that
the parentheses match, one set around unsigned,
one set around NULL, and then one set
around the entire thing, which is the srand function.
What this function does is to fetch the current
tick-tock value from the computer's clock.
It's a number that's constantly changing.
Save the code.
Build and run.
And you see a new value.
Let's run it again to check a new value.
And it's another value.
It's close because again it's based on the tick-tock
value which is incrementing throughout the day.
Why are random numbers so important?
Because random numbers are the key to making
all computer games interesting.