Awesome, we can display the next person on the previous person and of course, let's work on the random
person functionality.
I think I'm going to come up right away with a function.
So we're going to go with CONSED.
And of course, we'll mindless, random, random person.
Now, that is going to be equal to, of course, my natural function.
Now, I'm not going to pass anything inside of the function, but just to showcase how the math random
works, we're going to go along and the math and then random and will right away invoke it.
And now, of course, where I have my button, I'm just going to go with on click not is going to be
equal to my random.
So now of course I'm going to navigate a bigger screen and then in a console you'll notice that as you're
clicking on random person, you get a random value and that value always will be between zero and one
hour.
It can be zero, but it will never be one.
So it can be zero point nine nine nine nine nine nine nine nine nine nine nine nine, but it's never
going to be one.
So how we can use this random value to get the random person from the array?
Well, there's multiple ways how you can do that.
But one of the ones that I like the most is simply by multiplying the value with a length of the array.
And then just make sure that number is always around it down, so we go here with LET and then I'll
call this random number.
Now I'm purposely using a lot and you'll see in a second why and that is going to be equal to a mouth
random.
So first, what I could do is multiply by the length of my array, which of course in my case is going
to be four because I have four items.
So then if we navigate here now and by the way, I didn't cancel my bad.
So we go here with a random.
No, here, and you'll notice that as I'm clicking now, I'm getting a random number between a zero
and four.
So the thing is, it is never going to be really four because again, I will never have one.
So if I multiply zero point nine nine nine something by four, it's always going to be somewhere between
three and then four, but it's never going to be four.
And what I would want to do instead is just always round it down.
So then the values will be between zero and three.
So essentially they're just going to give me all the indexes that I have in a row since I have four
items.
I have indexes between zero and three zero one, two and three.
This is exactly what I'm going to do.
So I'm going to have a math.
Math on the floor, and I'll pass in this value so I can multiply the random number by the length of
my array and I always make sure that I round it down so that just going to give me this random index.
So, again, if we now get here.
If we keep on pressing notice how we're getting values between zero and three, not one issue that people
have complained about is the fact notice, even though technically it is a random number, once you
click, you get some repetition.
And essentially what that means is that as you're clicking the button, technically, if the numbers
are exactly the same, well, the person won't change over here.
And in order to avoid that, what you could do is simply say, you know what, if the random number
is equal to my current index, well, then just add one or, I don't know, subtract one, whatever
you want to do.
So here, I'll say if.
And then random, no, not person, but around them and the number, if it is equal to an index, then
either add one or subtract one.
That's up to you.
You say random number is equal to index plus one.
And then, of course, I would want to set index, but make sure that you also go with it.
Check number where if you're adding this plus one, you still might run into this problem where the
value might be bigger than the index for the last item in the array.
So let's go with that index.
But instead of just passing random number, we also need to go check number and then only pass this
random number.
And that way what happens is that you'll successfully avoid that repetition where if I go with random
numbers, how each and every time there's going to be a random person, hopefully you're able to complete
the project.
And I hope to see you in the next project.