5. "Math and Random Numbers" 2025 Winter APS105 Section 3 (University of Toronto)
Watch on YouTubeVideo summary
To advance beyond basic arithmetic operations, the video introduces the C math library, which provides access to advanced mathematical functions such as square roots, exponents, trigonometry, and logarithms. Accessing these capabilities requires including the header file `math.h` at the beginning of a program using the preprocessor directive `#include`. This instruction tells the compiler to copy function prototypes from an external library into the current code before compilation begins. The video explains that function prototypes define the input types, output type, and optional argument names for functions like `sqrt`, `pow`, or `sin`, allowing programmers to understand how to call them correctly without needing to write their own implementations. For instance, calculating a square root involves passing a double value as an argument, while computing powers requires two arguments representing the base and exponent respectively.
The lecture also covers specific behaviors of floating-point arithmetic within this library, particularly regarding invalid operations like taking the square root of a negative number, which results in "NaN" (Not a Number). Furthermore, it details various rounding functions that handle halfway cases differently: `round` rounds away from zero, while `rint` uses scientific rounding to round toward even numbers. The video demonstrates how standard formatting specifiers in `printf`, such as `%d` or `%f`, do not actually change the underlying value of a floating-point variable but only affect its display; truncation occurs automatically when converting between types unless specific functions like `ceil`, `floor`, or custom multiplication tricks are used to achieve precise rounding to decimal places.
Finally, the session explores generating random numbers using the standard library function `rand()`. The instructor clarifies that these are pseudo-random numbers generated deterministically based on a seed value set by the `srand()` function; without changing the seed (often done automatically from system time), running the program multiple times will produce identical sequences. By combining `rand()` with the modulo operator, developers can constrain random outputs to specific integer ranges, such as simulating change-making scenarios where amounts are rounded to the nearest nickel or generating numbers within a custom interval using the formula `(rand() % (b - a + 1)) + a`. This combination of mathematical tools and random generation capabilities equips engineers with essential utilities for solving complex numerical problems in C programming.
Read the full video transcript
[Music]
e
oops alrighty welcome back to APS
105 apparently we're not all supposed to
be here all right so we left off last
time we could do some multiplication
some division some modulo some addition
some subtraction but we are Engineers we
probably want more math we can do so
that's what we get into today and as an
Addam added bonus we can start some
random
numbers so C also comes with the math
Library some of you tried to use it in
lab one and you were not allowed to
because we have not introduced it yet so
we will start introducing it today so
for more math more advanced than just
simple arithmetic we need to use this
Library so to add all function
prototypes from the library to our code
like how we were had to add something to
use print F we can do # include math.h
on a new line before creating the main
function so just to be clear a function
prototype tells you the inputs to the
function and the output so there's also
something come up that may come up
called a fun function declaration it's
almost the same don't worry about the
difference for our purposes if someone
says function prototype or function
declaration they mean this same
thing so like I said a function
prototype tells you the input and output
types so all function prototypes have
the form output type goes first followed
by the function name same rules as
variable names and
then uh parentheses and then all of the
input types and again void is a special
type that means nothing there can be
multiple input types separated by commas
and again input types can be void
meaning there are no inputs or it could
also be void in the output type meaning
no
output so some examples of function
prototypes are well I can write double
fu fu is typically just a madeup
function name we give just for
illustration purposes so I could say FU
and then in parentheses I can put a
double so this function prototype means
means there is a function called Fu that
takes a single double as an input and
outputs a
double this one means there's a function
called Fu that takes two doubles as
inputs or there's two double arguments
if we want to be fancy about it and
speak like a c programmer and outputs a
double if I write void Fu parentheses
double that means there's a function
called Fu that takes a single double as
an input and outputs notada
nothing and then if I have the last
variation here if I have double Fu void
that means there's a function called Fu
that takes no inputs and outputs a
double so a function prototype can
actually name arguments so you actually
know what the arguments are actually
doing so for example I could write
double Fu double X comma Double Y the
names are just to help you
understand really at a high level it
means Foo still takes two doubles as
arguments and outputs a double but now I
just gave them names so the first double
is called X and the second double is
called y they again are just to help you
understand which argument is which and
the order actually matters because it
has to match whenever you use this
function so whenever you use the fun
function your variable names do not have
to match whatever is in the Prototype
just the values and the compiler calls
these values again literals if I just
write a one or a 2.0 or something like
that
so how the C compiler works so this is
kind of breaking apart what the C
compiler job is it's not testable but
we'll actually understand or help you
understand what's actually going on when
you compile your program so anything in
C that you write with a hashtag it's
technically called a pre-processor
directive that's just a fancy word of
saying hey this is something that
happens before c actually compiles your
code so there's something called a
pre-processor that runs before your code
gets compiled and this hashtag tells
your pre-processor what to do so it will
make some substitution
so if I see # include basically all that
will do is replace that line with the
contents of the specified file so it
will do like a copy and paste for you so
that
std. or sorry
stdio.h that's a file somewhere on your
computer that spoiler alert has a whole
bunch of function prototypes in it like
for print F and before you program
program gets compiled those get all
copied into your program so you don't
have to write them and then you can use
print F in your program if you delete it
there's probably you're probably going
to get an error message of the C
compiler saying hey I don't know what
the hell print f is so header files for
the most part contain prototypes of
functions and by convention any file
that ends in a h like again that stdio.h
or PD li. those are called header files
that's what the H is supposed to stand
for why are they called header files
well because they're supposed to be at
the top it's the header if I'm writing a
letter or something so a library
typically comes with one or more header
files and the header files contain
function prototypes for all usable
functions in that Library so we can see
what we can use in the math Library so
one of the function prototype in the
math library that we are allowed to use
is well a function to compute the square
root of something so in math I'd write
you know the square root symbol X in C I
have to use a function name I can't just
write some squiggly lines and hope it
understands it so the function prototype
for the square root function is double
they had to shorten it of course so they
shorten it to
sqrt short for square root again C
programmers are lazy and they don't like
typing very much so this is just what it
is and it takes a single double as an
argument or as an input and it's called
X so this should take the square root of
x and the output should be the result of
doing the square root operation so if I
do square root of four it should give me
the answer to what square root of four
is which is two
so 2 actually use that in a program I
could do something like this
so again the new thing I have now is
this line at the top that has hash
include math.h so that brings in the
function prototype for square root so I
can actually use it and then I can
expand on what I did yesterday I can
declare a variable called y that is
equal to 4.0 so it's a double and then
well Square root outputs a double so if
I want to actually capture its output I
can create a new variable called Yore
square root and that will hold I will
assign the result of the square root
function to that variable so I call
square root and I give it as an argument
my variable y so by default my VSS code
extension will tell me the name that the
function prototype used uses here in
this kind of gray text so the gray text
is just to help you understand which
argument is which that's based off the
Prototype your names do not have to
match this so this is not actually
present in my code as I'm typing it it's
just what my editor is telling me to
just kind of give me a hint as to what
argument is which so the X argument to
the square root function I'm going to
use my value of y for it which is four
and then I ass the value of the square
root function so the output of the
square root function which should be 2.0
to this yqu root function and then if I
was to print the result I could again
print F I could use the format
specifiers for a double and it will do
them in order so I could print the
square root of the first argument which
is y so I will print it using this
format specifier which again just only
prints off a single digit after the
decimal point and then I can say
equals again single digit after the
decimal point with whatever the result
of that function is so like I already
ran before but if I just ran it again it
will print Square < t of 4 is equal to
2.0 so any questions about that yeah put
like a negative number so what if I put
a negative number so we can just see
what happens if I do the square root of
a negative number undefined right or
it's imaginary number so let's see what
C does so if I just do it I get this
special value where it says Nan and
you're like what the hell is a nan
that's not a number and the answer is
exactly Nan stands for not a number so
if C can't represent something there's a
special double value that means not a
number so if you do something invalid it
says not a number for anything we do if
we ask you to use square root
we'll make sure it's positive but yeah
things will
do odd things depending on
it um any other questions
yeah a yeah so the question is why did I
why didn't I specify that Y is a double
so in this case I don't have to specify
Y is a double explicitly the function
arguments just tell me what the type of
Y has to be or the first argument has to
be so that X needs to be a double right
so in this case if I look at y y is a
double so they agree so as long as they
agree that's okay so if I said why let's
say I declared it as an INT
instead see what my compiler does so if
I declare it as an INT instead turns out
my compiler doesn't complain at me or
anything like that if I compile
it nothing oh it does complain so part
of it complains so it doesn't complain
about this but what will happen now is y
is an integer and this square root
function needs a double as its input
right so what c will do is it will
implicitly convert that in to a double
for us same like when we were doing math
so it will take that four and convert it
to 4.0 and then it'll do
it the
begin to
thats line a dou yeah so the double at
the beginning of line seven that says
what the type is of this name right and
then since I'm assigning the output of
square root to that and it's a double
they agree so the output of square root
in this case is going to be 2.0 so it
assigns it to Yore square root and if
they don't agree sometimes c will just
do the conversion for you and yell at
you in this case it's yelling at me that
why is now an INT so Y is now an INT and
what my compiler is trying to tell me
that it knows that this first format
specifier is supposed to be the format
specifier for a double but it's telling
me that this is an INT so they don't
agree anymore so weird things might
happen so if I want to make my compiler
happy and still use uh y to be an INT I
would just remember my format specifier
for an INT is just D and then I could do
this and make my compiler happy run it
and now it's going to say just four
instead of
4.0 any other questions with what we are
doing
yeah
sorry so what x stands for is in the
prototype in this math.h Library someone
defined print or square root something
similar to
this so they said that square root it's
a function that takes a double as an
input and they just called it X so
that's just the name they give it so
that it makes sense to you hopefully and
my editor is just showing me this
whatever that name is here but it's not
actually like I didn't actually write
that X there yeah so that X is just my
code editor telling me like if I had
multiple of them uh we might see it with
a different function but if I like had
say this took like an X and A
Y right I Pro I probably don't want to
go back to here to figure out that the
first argument is X and the second
argument is y I probably want some help
from my Editor to tell me which is
which all right any other
questions all right cool
so so just so you have in the slides
that's the example that we just went
using a square
root so
if I wanted to and knowing some special
things about what's in the math Library
just to show you this as an example I
could create my own header file in a if
I wanted so I could create a header file
called square
root. with the following content so I
could just put inside of it oops I could
just put inside of it that square root
definition so here in my vs code in that
same directory I've Square Ro t.h and
here I have that function prototype for
just square root so function called
square root that takes as an input a
double and outputs a
double so if I do that create a file
called Square root. H well if I want to
use it oops so if I want to use it in my
C file I can just do hash include and
then the convention is or the rule are I
use double quotes for anything that is
any header files that are in my
directory so in this case I just do
double quotes and then Square Ro t.h and
that will essentially copy paste the
contents of that header file into the C
file whenever it compiles so let's just
get rid of this line because it might be
confusing so if I do that and compile
nothing
changes um it's just square root oops
square root
custom header still works my compile my
program still compiles everything's okay
I just don't have math.h there and I
just did the function prototype myself
if I wanted to again this is not super
important for this course you will not
be writing header files but that's
basically they did so basically how it
works is before your code got compiled
the contents of this Square Ro t.h
was
just double Square
t double like that
so whenever your C pre-processor runs it
will include like the contents of those
files in this C file before it actually
compiles it these files are absolutely
huge so I won't show what they do but
that including Square Ro t.h is just
going to copy and paste basically copy
and paste everything from this file
which is this one line and replace that
include with the contents of the file so
it would just do this before my compiler
runs and then it would compile this and
then C is happy because it knows what
the inputs are to the square root
function and I can go ahead run my
program again like nothing is wrong so
just another way to do that a bit above
what we need to know for this course but
ju again just in case you are interested
all right
so again like I said before compilation
the pre-processor does all the
replacements for you and like I said in
that case the result after The
Replacements are just that square root
function declaration gets included there
if I gave a name to the input and called
it X it would go there so the convention
is if that file is in angled brackets
that tells the compiler to look for the
file in like the default Library
directories that exist on your computer
and just double quotes file means look
for the file in the same directory as
the
code all right other fun functions we
can use is here's one that takes
multiple arguments so we can have a
function for exponents so in math I
would write x to the power of Y in C I
need to use a function they didn't call
it exponent they called it pow short
power
so this is a function called pow that
takes two arguments they're both a
double the first one is called X the
second one is called Y and it will do
exactly x to the power of Y returning
you a double as a result so if I did pow
2.0 comma
8.0 that would be 2 to the^ of 8 which
is 256
so questions about that that's just
another function with different
arguments uh do I have an
example well we can just use it here so
how my editor would help me is maybe I
don't remember which is X and which is y
so I could just print
off let's just print off whatever the
result of some power operation is I
could do pow in this case my editor will
help me and this case it's not telling
me the names oh because I didn't include
uh math.h let's go back here so I
include
math.h then my editor is probably going
to help me out a little bit yes indeed
so it'll pop up and tell me which
argument is which so it will show me the
inputs to the function and a little
description there return x to the Y
power so I could do 2.0 for x and then
again we can give a answer your number
10.0 for y and then end it up and then
my editor
will show me which argument is which
again I didn't write X or Y it's just
giving me the names to them so at a
glance I can tell that I'm doing 2 to
the power of 10 so if I compile and run
this now I can see that the answer is
what I expect
10,24 questions about
that cool another function
down let's try and make a record so
there's trigonomic functions so we love
s and Co and all that stuff so here's an
example using S so s it's more of a
function so this is a function prototype
for something called sign takes a a
single argument which is a double in
this case it's in radians and then it
outputs a double also in radians so in
math.h
if I want to use Pi I probably don't
want to remember
3.1415
92
654 I still remember that for some
reason I don't know in grade six I decid
to remember pi to like 10 digits I don't
know I was a weird kid I guess that's
why I was in engineering so if you don't
want to remember that the C developers
help you with that and they have a
basically a constant variable for you to
use so if I want to use Pi in my code I
can use the value mcore Pi all in
capitals so
technically it's not a variable it's
like a pre-processor thing but the
compiler will replace or the
pre-processor will replace mcore Pi for
you with the value of pi and the value
of pi in C is as many digits as a double
will hold so it's uh up to 21 digits of
pi which is more than I know
so for example if I wanted to take the S
of 90 or whatever that's supposed to be
I forget if I wanted to take the S of Pi
/ 2 I would just do sin M Pi IDE 2.0 and
that would just give me the answer of
one all right so there's also
logarithmic functions to in math if I do
the natural log or log base e in C I
need to use log annoyingly it's not
called Ln and log is not base 10 you
just have to remember these things so in
C log which takes again one double and
outputs a double that is the natural
logarithm or to the base e of x if I
want to use e like the value of e which
is 2 point I forget I haven't used it in
a very long time you do capital mcore E
and the way to remember that is any
constant numbers in the math Library
start with M underscores so mathcore Pi
mathcore e so if I want to do log base
10 or normal log in C there's a function
just called log 10 so log
10 annoyingly log is not log 10 log 10
is log 10 so this will do the common
logarithm logarithmic
logarithm to the base 10 so of x X so
it's just log 10 just remember that that
they're slightly
different all right so there might be
the case where using floating point or
using doubles I want to get the
remainder still and remember we can only
use mod to get a remainder using
integers so if I still want that I have
to use a function in this case they
called it fod which is short for
floating mod modulo if you want to
remember it that way so this will
compute the remainder of x / y or sorry
of x / Y and it returns a single number
which is just that double so in the case
if I want the remainder of 3.5 / 2 I
would do fod 3.5 comma 2 and then the
result which I just show as an arrow
here like I've been doing for the rest
of the slides sorry just in case I
didn't explain it the remainder is just
1.5 all right other fun ones you can do
that might be useful is I can get the
minimum and maximum of two values so if
I want to know between two values which
one is the lower one or which one is the
minimum I can use f Min it takes two
arguments so in this case they just
called them double A and double b and it
returns a double so it will return
whatever the small smaller value is of
the two and on the flip side if I want
to see what the larger value is of the
two I could use f Max so again it takes
two doubles as inputs and outputs a
single double what it will output is the
larger of the two values there so this
probably isn't useful if both arguments
are literal values like it's probably
silly if I hey C what's the largest
number between one or two I should
probably be able to figure that out so
this might be more useful if the
arguments are variables and you don't
actually know what the values are so
here's some examples so again for this
the order of the arguments don't matter
so if I do fmin 2.0 comma 3.5 or 3.5
comma 2.0 it doesn't matter it will
always return the smaller of the two
values so in both of these cases if I
use f Min it returns 2.0 if I use f Max
again using the same arguments either
2.0 and 3.5 or 3.5 first or 2.0 doesn't
matter it's going to return the larger
of the two values which in this case is
3.5
yep yeah so the question is can I use
more than two values and for this no
it's just two values for this one
because that's what the Prototype is
only has two values
there are some special functions like
print F that let us have more values but
this is not one of
them all right other ways all right so
getting into rounding some of you got
ahead of yourselves in lab one and
wanted to round things so there's
multiple ways to round to the nearest
integer in C and the proper way to do it
comes from the math Library so there's a
function called round that rounds and
computes to the or rounds to the nearest
integer and it round rounds away from
zero in halfway cases halfway cases are
the0 five so rounding away from zero
means if they positive numbers it will
round up if they are negative numbers it
looks like it rounds down so if I round
use round for 2.5 it will round up to
three if I use round fortive
-2.5 going away from zero means it will
go to -3 there's also R in which does
the more same scientific rounding so
that will compute the nearest integer
given to you as a double and it rounds
towards even in halfway cases so if I
round 2.5 it will round to two if I
round 3.5 it will round to four and that
is by default there's ways to change its
rounding Behavior but we're just going
to use the default rounding so for the
most part for this course our in does
more of the proper thing we want it to
so here's some examples
again rounding to 1.5 again it will
always round away from zero so for
positive numbers it always rounds up so
it Round Up to 2.0 if I do round of 2
point or
2.5 the result will be 3.0 if I use rint
for 1.5 it rounds up to 2.0 and if I use
R imp for 2.5 which is the halfway case
it will round towards even which is 2.0
which is like the proper scientific
rounding method so questions about
that
cool
so again like we pointed out before uh
the Double format specifier does not
change that value so to illustrate that
I can just show you what it will do in
this case so in this case if I Clare a
variable called a and set it equal to
2.5 then the value a is equal to 2.5 if
I print f with a format specifier that
will just change how I'm outputting that
value it won't actually change that
value so if I do print F and do the
format specifier 0 Z so output no
numbers after the decimal place and
output a it will actually just print to
so it will do the default truncation
thing in this case for displaying it
won't round or do anything like that so
it's just going to Output two and then I
didn't actually change the value of a so
if I do print f. one so print one digit
after the decimal place and that's the
rest of the for format specifier double
I give it a a still has the value 2.5 so
this would actually print 2.5 so
that 0 Z the rounding use is technically
our in or sometimes it truncates if
there's more digits um this 0 Z thing
will always round towards zero so it
will not try and round it will
essentially just
truncate
so if I want to do some more rounding
those rounding functions by default they
only just you know they only round to
the nearest integer number if I wanted
to round to a different decimal point
well I could use the printf specifier
like you probably did for or what was
recommended to do for lab one so again
if I want if my value is
2.55 and I do it to one decimal place
this will actually print 2.5 and and if
I do
this only print off a single decimal
point and the value is
2.65 it again will do the r in thing and
round towards the nearest even value so
this will print
2.6 so again the rounding rules only
apply to the nearest integer so if I
wanted to round that properly or round
it differently I have to do a little bit
of a trick I have to move the decimal
place by just moving it by or
multiplying it by 10 so if I multiply a
number by 10 that will move the decimal
place one place to the right and then I
could in this case it would change 2.55
to
25.5 then I can use the round function
to turn that into an integer which would
round it to maybe 26 if I just used the
round
function or if I used R int and I always
wanted that to round to the proper thing
then I just use R int on that value it
would properly round it in this case to
26 in both cases and then to get the
original value back I just undo
multiplying it by 10 so I can divide it
by 10 again so the digit I want to round
it should be the first digit to the left
of the decimal so I just have to move
the decimal by multiplying by some power
of 10 so moving it by one place multi
multiply by 10 if I want to move the
decimal by two places I multiply by 100
so after rounding so I do r in it turns
it into an integer it is now properly
rounded and then I divide by that same
number to get the original decimal point
so here I am trying to round 2.65
properly to one decimal point so I
multiply it by 10 that means I would be
rounding 26.5
since our int
always uh rounds to the nearest even
number the result of this is
26.0 then I divide by 10 to get 2.6 and
then now it is properly rounded so print
2.6 and similarly to
2.55 multiply by 10 again like above
25.5 R int would round to 26 and then I
divide it by 10
now I finally get it to tell me the
answer is properly
2.6 so any questions about that
fun so one little trick we have to
do
so there's some even more fun ways to
round to the nearest
integer so in math if you've seen the
symbol before like the little hat
bracket things that's a ceiling in C I
can do the same thing but the function
is called seal short for sealing and
that will always round up to the nearest
integer no matter what and in math if I
write floor the symbol for floor in C
the function is just mercifully called
floor they actually wrote it out this
time so ceiling will always go up to the
nearest uh the next highest integer so
ceiling of 2 point or 1.1 goes up to two
ceiling of 2.0 it's already in an
integer so it'll leave it alone and if
it's a negative value it always
increases so if it's 1.9 it will go up
to the next integer which is just
negative uhga 1 similarly for floor
floor of 1.9 again it basically does the
same thing as truncation so it will just
chop it off go down to the nearest uh
integer which is 1.0
and for 2.0 again it will stay the same
floor does not behave like truncation
for uh negative values because it always
goes to the lower value so if I do floor
of - 1.1 it's kind of in between -2 and
-1 the result of that will be -2 so it's
slightly different than truncation it's
the same for positive numbers
though all right so truncation again
again similar to floor and ceiling so
truncation technically Returns the same
as floor for positive values if you want
to remember it that way and sealing for
negative values but again truncation is
what default happens when C converts a
float to an integer we just chop off the
decimal place if you want to remember
like if you want to remember it like
that instead so truncation of 1.1 cuts
it off it's just one two is just two -
1.5 is just chops off the 0.9 so it's
just -1 and again same thing for those
values all right so there's lots of
other different math functions available
you can see the entire list on Wikipedia
again this is clickable on my links for
this course you should only use the
functions that use double for all the
arguments and outputs so for some lab
possibly lab 2 you might find F abs
useful that will give you the absolute
value of a number so there's a whole
bunch of them that might be important
for
you so we can use this to solve some
problems now so for instance let's solve
a problem where we're making change
using nickel so given an amount from the
user in dollars and cents we want to
round to the nearest nickel
so for example here is the default
sketch of the program I have
I'm going to ask the user to input an
amount in dollars so my type of that is
going to be a double it's going to have
a decimal place I'm not going to be
concerned about invalid values or the
user typing something silly again we're
going to be nice to you and only input
proper things so I'll do a scan F to
actually capture whatever the user
inputs into this double variable called
amount and then I have a to-do where my
job is to round to the nearest nickel
so what I like to do when I program is
like think about what the user might
type and what I should output so some
inputs to my program that I might want
to think about are
like if the user inputs something like
42.4 2 I should think about what my
output should be and in this case if I'm
rounding to the nearest nickel well my
output should probably be in this case
42.4 right that's rounding to the
nearest nickel and maybe I want to think
about another case where I round up so
maybe my input is 42.434652
problem knowing what we know about
rounding now anyone think of something
we could do to write a program to round
our amount to the nearest
nickel I'll give you a a minute or two
to think about it what we could do to
round to the nearest
nickel yeah we'll wait 30 seconds and
I'll get you
okay all right what's your idea
okay so your suggestion is to multiply
the amount by 200 and then round that
and then divide by 200 why 200
okay
so so we want to
essentially take the in amount we got
from the user and then first to break it
into two parts maybe multiply it by 100
to get the dollar
amount or no to move the decimal so that
it would be like you know in this case
would
be
4242 right so that's the number of
cents and then multiply by two or total
of 200 to
get because you said multiply by 200
right yeah you're getting close to the
nearest
Fifth
so any other ideas so that's kind of
there I'm kind of thinking about how
many nickel I have yet multiply by
multiplying by 20 okay so multiplying by
20 so you want me
to multiply it by 20 let's just see
so
so uh let's just output so we're going
to Output a number to two decimal
places and you want to take the amount
multiply it by
20 and then round
that and then divide by 20 something
like that all right well the idea is I
want to kind of like yeah get groupings
of the number of nickels I have and then
round that and then shift it back over
yeah
using the modulo
operator the double modulo to get the
remainder if I divide it by
0.5 and then round
that yeah I could do that well let's see
if this one works first so I'll compile
it and I called it my program just
nickels so enter on amount I'll do
4242 and I got
4240 so seems like it Works 42
43 42
45 that
works I would argue 20 in this case does
work a way I like to think of about
it that might be a bit more
obvious multiply it by 20 works or I
could do the opposite of that which will
essentially do the same thing which is
divide by the value of a nickel right if
I divide by the value of a nickel
basically I'm saying
how many
nickels is this amount and it will have
some decimal point like a decimal of a
nickel right and then I round that so
that rounds me to the nearest nickel and
then instead of dividing it by 20 I do
the opposite thing which is I mean it's
basically the same thing I'm just
switching divide and multiply but I
could multiply by the value of a nickel
to get the original value back so they
mean the same thing I basically just did
the reciprocal and did it this to me
makes more sense to me because it has a
nickel but like if you wrote 20 and just
wrote that as a comment and explained
your reasoning then that's perfectly
good too but they do the same
thing if I do this now compile it try it
again boom same thing all good we have
now written some software that is on any
cashier till if you pay cash on the
planet so
now you can get a job for a cashier
company I guess if anyone actually still
uses cash which I guess less and less
people do now but you've written some
useful software now cool all right so
any questions about that before we move
on to the last topic which is
fun cool so you can create random
numbers to the standard C library uh
defines a function called Rand again it
is in this
stdlib.h file so its function prototype
is Rand takes no inputs and outputs an
integer and it outputs a random number
between zero and this value that they
tell you uh inclusive so this is just
some big value that they have defined so
if you care about R Rand Max is actually
that value using most compilers and this
is a Max maximum value represented by an
in so it's 2 31 minus 1 so it's just the
largest positive value uh represented by
an in so always returns a value between
zero and that
number so it's technically a pseudo
random number generator pseudo means not
genuine like it's not real so Rand
actually uses a formula to determine the
next random number and it uses a seed
value to create a deterministic set
sequence of numbers so if any of you
have played games or played Minecraft I
guess that's a thing with Minecraft you
can set a seed to generate a level for
you and the same seed generates the same
thing every single time because it's not
truly random it's using a formula to
come up with a whole bunch of fake
random numbers so deterministic means
it's the same all the time same input
gives you the same
output so I can change the seed for Rand
using a function called s Rand so s Rand
changes that seed value that Rand uses
to generate quote unquote random numbers
they're not really random so it takes as
a single argument something new uh
unsigned in seed basically what unsign
it it represents a whole number and it
doesn't have a sign so it must be
negative and it still uses four bytes so
an unsigned int can technically
represent all it's like there's no sign
or anything like that it's basically
every bit is used as a number as we did
with our numbers column thing so
unsigned in can represent any number
from zero all the way up to 2 to the 32
minus
one so doesn't so there's some numbers
in an unsigned int that can't be
represented by an INT right anything
greater than 2 to the 32 so generally
because of this it's a bad idea to Mix
signed and unsigned
types int by itself is a sign type
because it can be negative for this
course don't don't ever try to mix them
always use an INT if you don't need a
decimal don't worry about whether or not
it can be negative or not we will tell
you whether or not it can be negative
don't try and be fancy or anything like
that so how I can use this random number
generator I can combine it with this
modulo operator to generate a different
range so modulo using an in gives us the
remainder after doing integer division
so since Rand returns a whole number the
rules with negative values don't apply
so I don't have to argue about anything
weird so if I just do Rand Mod 20 so
Rand will give us some again random
number that is between zero and
something huge if I do mod 10 it will
give us a number between 0 and 9
inclusive because that will be the
remainder of doing it and that remainder
will essentially be random so this will
give us a number between 0 and 9
inclusive so if I want to Generate
random numbers between 1 and 10 exclus
inclusively all I would have to do is
just add a single one to
it so if I do Rand mod 10 + one again
that will result in a random number
between uh 10 or 1 and 10 inclusively so
I could use it like that
and that would be perfectly
fine so here let's see some examples
before wrapping up so here's R
whoops so here's Rand just generating
random numbers we can see that they're
all sorts of random giant values that
happen to be well in that time they were
the exact same because it actually
determines the seed from the second time
on my clock so if if you run it within
the same second it'll generate the same
number actually it doesn't actually
randomize the seed at all apparently on
this operating system so I get the same
numbers every single time it's not truly
random so I could initialize it to some
random seed if I wanted to change the
random numbers so again I could use S
Rand maybe change it to
four and now when I run it I will get a
completely different set of random
numbers because I changed the seed so
now I have a different set of random
numbers but every time I run my program
they're going to be the same so to
summarize this will be a useful formula
for you if you can't remember to it so
to generalize generating a random number
if I want to generate a random number
between A and B inclusively where a is
greater than B I can use Rand mod a
minus b + one that gives me my range and
then plus a to give me that starting
number and adjust it back so just
remember pulling for you we're all in
this together