ok, let's wake up
we just kind of get this idea
we did something so small but so meaningful
which was to introduce this idea of a variable
now you might feel a little bit sad
with just this grey circle moving on the screen
and there's not really a lot happening
and you want to have more splash, pizzaz
in your program
let's look at something that will get us
practicing with variables a bit more
and allow us to introduce much more variety into our code
and that is the use of the random function
i think this is really important for several reasons:
number 1 is that we now know that we have two kinds of lines of code
that we can write.
we have a function call, like line, from a bunch of values
to other values, and we also know we can have
assignment operations where we take a variable
and set it equal to something. we can even take a variable
and set it equal to itself plus something.
we can create all sorts of mathematical expressions
that allows us to assign a value to a variable
but there is another way, we can actually do both of these in one line of code
and I'm gonna show you what I mean by that.
we can say something like: x = random(100);
now look at this: this is both an assignment operation,
assign x a value, and a function call.
this is a function, it has a name, and arguments inside parentheses
but what's so interesting about this
is random() is a different kind of function than line().
and we can think of it this way:
when I call the function line() I issue a command, a decree in a way;
draw a line! when I call the function random() instead of issuing a decree
I am actually asking a question:
oh, function random(), could you please give me an answer to the question
'what is a random number between 0 and 100?'
and the random function will say: 'aha, here is a random number between 0 and 100: ... 27!'
and it will give me that number back and we're gonna story it in the value x.
this is incredibly useful! in fact there are lots of methods
in Processing, I think we need to make a video just for this one,
that work this way. there's a function called map(), there's a function called constrain(),
there are a lot of functions that manipulate values for you and return back the answer
to a question in a way. a random number between some minimum and some maximum.
how is this useful to us?
so let's look just at ... so, first of all, let's just be a little bit more specific
about how the random function works.
the random function can take 1 argument, so that argument, let's say I put 100 in here,
is the maximum random value, this will give us a random value between 0 and 100,
a minimum and a maximum.
it can also take two arguments: 50,100 for example
this is the minimum and this is the maximum
it will always give us a random number between 50 and 100
now the other thing I should point out about this:
it's not giving us 51, 52, 53, 54, 55
the random function actually gives you a floating point value back
so in fact it 's almost giving you an infinite number of possibilities
we're gonna get values like 59.231789 we're gonna get 79.003512
we're going to get floating point values.
this is fine, for us, if we've declared our variables as floats
we can get a float from random() and everything's ok
there are some cases when you need to take the result of random()
and convert it to an integer, and you'll see that in examples in the future
we do not need to worry so much about that right now
so this is how the random() function works.
let's go take a look at that in our program!
we walk over here, awkwardly, ...
ok, so for example, one thing we could just do really briefly, really quickly
is say: let's random(0,width). So I actually wanna give circleX
a random value between 0 and the width of the window
and I'm gonna do something else, I'm gonna turn off the circle's motion
by commenting out that line of code. this is often very uselful,
if you wanna keep a line of code kind of in reserve but want to deactivate it
you can just comment it out so it's commented, meaning the compiler
will ignore it when it runs. so we run this program
look, there's our circle, it's a random location.
let's run it again!
woah, that was like the same location twice! what's the odds of that?
let's run it again and really hope we get something pretty different!
ok, I feel better now. phew, there it is over there!
we will get a new random value each time the program runs.
now you may ask yourself: is it REALLY random?
like: how random is it, you know? so the one thing we should
just briefly mention is: this random number comes from something known
as a pseudo-random number generator. it is a mathematical function
invented by people, perhaps you could spend your life kind of developing
pseudo-random number generators, you could ... years of study ...
but we get one for free in Processing
it is not perfect randomness in the sense of what you might find
in some occurrence of nature, I don't know, if you heated up some gas
and watched how all the particles moved around ...
and get some sort of more pure random out of that
but it will exhibit some kind of pattern over time
but we aren't ... we're writing, you know, sketches that draw little circles
and colors on the screen, it's plenty good random for us right now!
but it is coming, it is just a mathematical function
written into Processing that gives you a random sequence of numbers
there's other controls we can impose over that random as well
which are more advanced, that I would love to discuss with you
some time if you'd like to talk about that!
ok, so now that we have that, that's pretty pretty good
but let's think of something a little more interesting
(talking about pen)
let's have our circle start in the center of the window
so the circle should start at width/2, oh, ha, I got something for you!
remember how I was saying everyone just wants to give circleX's
initial value up here, cause that's so much more convenient
just to do it in one line of code? but I just want to give you a warning:
this will not work for you now! this will fail
because the width has not been established until size() has been called
this is why you wanna be careful about initializing all your variables up here.
sure, it's more convenient! and it's less code! less typing!
most of many examples you'll see do that but this is a nice instance
where circleX should be initialized here
ok, so we've established circleX is going to be in the middle
we're going to move circleX again
right, it's moving by 1 but what if I said this?
move circleX by a random value, and I need a little more space here,
between -2 and 2, right? a random number between a minimum
value, -2, and a maximum value, +2
Sorry, the minimum value is -2 , the maximum value is 2.
instead of saying: circleX = circleX +1 every time
through draw() we're going to say: circleX = circleX + 1.5, sometimes
-0.3, and sometimes .x, sometimes -.x
and if we run that, think about what it's gonna do:
we run it
look what's happening
zoom in on that ... oops ...
the circle, what is it doing?
it's moving randomly each frame, in a different direction
so it gives us this jiggling like quality
what if you had its size change randomly each frame?
it's gonna have this almost ... nervous looking circle on the screen
and it would be nice, maybe we could think of ways
to calm it down, to soothe it, and be more gentle to it
but this is kind of interesting:
random allows us a lot of possibilities
beyond 'just give it a random location where it starts'
and that is giving us the inkling of that how you can start to create behaviors,
personality in a way, to the shapes that you're drawing on the screen
so, another thing that I just thought that I would briefly show you
I'm gonna save this as random2, I guess what I might do is
all these examples are essentially similar to other examples I'm linking to
but I might typing them out here and they're slightly different
I might, I put them somewhere else with the link ...
uh, I don't need to be telling you this right now but ok
so let's just look at one other thing,
so here's an exercise that I might give you
which is: use random to affect the x, the y, the width and height
of a shape and see what type of personality you can give it.
here's another exercise to see if you can use random to essentially
create a painting on the screen.
and let's think, what are we gonna do, let's do a pointillist painting.
so, the first thing I wanna do here, and I'm gonna get you started on this
is add 'circleY'. and the second thing I wanna do
is draw circleY, uh, draw the circle at that location,
and I'll give them both initial locations
and i'm gonna take out this line of code completely
and I'm going to run this program
and what do we get? we get a circle that's in the middle of the screen
it's x and y, width/2, height/2, there it is.
now, what if instead of assigning the initial value in setup
I give it a new value every time through draw()?
and I say: hey, give it a random value between 0 and width
(phone blabla)
ok, a random value between 0 and width
and circleY a random value between 0 and height
now we're doing that EVERY time through draw()
instead of giving it a random value ONCE, and it sticks there,
now we're giving it a random value every time through draw()
look, it puts it in a different location every time
but what if ...
I took the background out and only draw the background once in setup()?
now we're seeing the circle appear
over and over again at different places
what if I gave those circles a little bit of alpha?
what if I made the size of the circles random?
what if I made random values for the color of each circle?
could I create some type of painting where all these random colors are blending?
could I restrain the randomness? could I say the red is just between
50 and 100, and the green is between 150 and 175 to kind of
create a palette of colors?
so this, I think, is a nice exercise for you to kind of play around with
a. can you control the motion of a singular shape through random?
and also could you create kind of this painting, this collection of stuff
piling up with random as well and use alpha and color and size to really create
some kind of pattern? you know use lines, rectangles, other shapes
besides circles? so that should get you kind of going with some stuff
to play with in terms of using random()
and, uh, that's what I got to say about that!