Almost every game you play is going to have random within that game to make it more interesting because
of course, if you have the game, it's really predictable.
It's not going to be that much fun.
And this is where we introduce random.
And with JavaScript, we do have the ability to use math random.
And what that does is that's going to return back a random value anywhere from zero to one within your
JavaScript.
So we'll create a brand new variable.
We can call it computer selection, and we're going to use the built in Amarth random function within
JavaScript.
And I'm going to output this value into the console so that we can see that it's always got a random
value.
So whenever we click one of these buttons, you can see we've got this random value and this value,
as mentioned, anywhere from zero to one.
So we know that 33 percent chance of selecting rock, 33 for paper and 33 for scissors.
So using this formula, we can determine what the computer is going to select.
So taking that value of computer selection, we're going to check to see if it's less than zero point
three four.
So it's roughly around a third chance of the computer selecting that value.
And if it does, then we can set up a new variable or we can just use computer selection and equal that
to rock.
And it actually doesn't matter which one you use using else.
If so, another condition we're going to check to see if computer selection is less than or equal to
zero point six seven.
So we know that the first one is checking to see if it's less than this third.
And if it's not, then we're going to check for the next third.
And we already have pulled out the selections that match less than zero point three four.
So we've already got that third listed as rock.
So we only have a third left here.
And the point, six, seven.
So using that same formula, we know that if it lands on this, then we can make it another one of the
options.
And for this time, let's just do paper.
And if none of that criteria matches of computer selection, it doesn't match any one of these, then
we're going to simply select computer selection to scissors.
And this will give us roughly around a 30 percent chance, a 33 percent chance on any one of these items
for the computer selection.
So I'm going to take that computer selection.
I'll move it down here.
And we know that the user has the entire text so that one is selected by the user and the computer selection
is going to be at random.
So we hit rock.
Both of them got rock.
If we had paper, the computer got scissors, we got paper.
If we picked scissors, then we know that the computer got paper.
So these are all at random.
So it doesn't mean that if I hit scissors, it's always going to be paper.
And that instance that seemed to occur that way.
But we see sometimes we had scissors and then we get the computer returning back rock.
So that's how you can introduce a randomness into the selection process, into the gameplay.
And this is, again, a really crucial part of almost any game that you're going to be developing within
JavaScript.
So go ahead and add this in using conditions you can check to see what the value is and then update
the computer selection value accordingly.
And then we're going to be ready to move on to the next step where we're going to check to see who wins.
That's, again, really important part of the gameplay where we need to have some conditions for winning,
whether it's the player or the computer.
So that's still to come.