Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Start a free Basic trial
to watch this video
A random number generator function uses an algorithm to generate a random number within a given range.

0:00
We are very close to getting the basics of our app up and running.

0:04
Well, by basics, I mean, randomly displaying the prediction.

0:08
So, you can actually fool your friends into

0:09
thinking that your app can predict the future [LAUGH].

0:15
Sorry, where was I?

0:16
Oh, yeah, the concept of randomization.

0:20
Have you ever seen them pick Lotto numbers?

0:22
It's like a big game of chance.

0:24
At this hat here filled with some ping pong balls, and

0:27
each one of them has a different number written on them.

0:30
If I give it a good shake, there's a

0:32
good chance that I get a different number each time.

0:36
Well, similarly in computer science, there are

0:39
different algorithms to pick a random number, but

0:42
these are not truly random because we're using

0:45
a mathematical formula to create a random number.

0:48
Hence, these are called pseudo random numbers.

0:51
Now don't worry, we don't have to write any of these algorithms ourselves.

0:55
All we need to do is use a function that generates them for us.

1:00
As you can see, here is an array of predictions.

1:03
We use a random number generator function where we give it a max number.

1:08
So where do we get this max number from?

1:10
Well it's the length of our array, which is 10.

1:14
In turn, the function which shows a random number within a given range.

1:20
Kind of like picking your number from a hat.

1:24
Let's say we get a 5.

1:25
Well, we will take this 5 and access the fifth string from our array.

1:30
Technically speaking, it's the sixth, since the array starts

1:34
from 0, but you already knew that, didn't you?

1:38
To generate a random number, we will use a c function.

1:44
And there are several functions to choose from,

1:46
but we will use the function called arc4random_uniform.

1:53
So let's create an int local variable, and we will call this random

2:00
equal arc4random_uniform.

2:05
So, we need to pass this method with the maximum

2:09
number that it can use to generate a random number.

2:14
So what'll this do?

2:15
It'll give us a number between zero and

2:18
less than the maximum number that we provided.

2:22
Now, the maximum number in our case are

2:23
the number of elements in our prediction's array.

2:28
So we refer to it as self.predictions.count,

2:34
which gives us the length of our array.

2:38
Now we can take this random variable, and instead of hard coding this

2:44
value for object at index, we can pass it that random number.

2:51
So now, anytime a user clicks on the Predict

2:54
button, it'll randomly display one element from the prediction's array.

3:00
So let's go ahead and run our application once again.

3:04
And let's see if this works.

3:05
If I click on Predict, it says, it is doubtful.

3:08
If I click again, it says, my reply is no.

3:12
If I click again, it says, it is decidedly so.

3:16
So now we're kind of getting that semblance of,

3:19
hey, our application's actually, you know, fake predicting the future.
You need to sign up for Treehouse in order to download course files.
Sign up