This forum is now read-only. Please use our new forums at discuss.codecademy.com.

# I am confused by why/how this code works

The code for this lesson is:

<!DOCTYPE html>

Coin Flips

We are going to flip a coin until we get three heads in a row!
<?php
\$headCount = 0;
\$flipCount = 0;
while (\$headCount < 3) {
\$flip = rand(0,1);
\$flipCount ++;
if (\$flip){
\$headCount ++;
echo "H";
}
else {
\$headCount = 0;
echo "T";
}
}
echo "It took {\$flipCount} flips!";
?>

I get most of it. The only thing that stumps me is if/else statement. If (\$flip) is set = to rand(0,1), then EVERY case will be (\$flip) because you'll always have a random value between 0 and 1. As such, it would seem that with this line of code, every flip would be heads. So how do you get a tails flip then? It doesn't make sense to me.

Thanks,
Repose

1 vote

permalink

I think the rand function isn't actually randomizing between the numbers 0 and 1. The numbers 0 and 1 in the rand function could possibly not represent values but instead represent states of true and false.

I understand your plight with this, because having knowledge of the fact that a 0 and 1 could also represent states of true and false requires some low-level knowledge of computers and the rand function situation isn't explained either.

Your best bet would be to go see how the rand function works in the PHP manual on their site.

##### 5 Comments

Mike almost 5 years ago

My problem is even more basic, I am stuck on a concept! Having set \$flip to a random number in the range of 0 - 1, what is \$flip then being tested for?

\$flip = rand(0,1);
\$flipCount ++;
if (\$flip){

It cannot be "true" or "false" as we have set it to be 1 or 2, so why does the test work?

cseto over 4 years ago

Looks like it's not a range. rand returns either 0 or 1; 0 being false state and 1 being true state. The 0 and 1 business stems from the binary language.

Alex Jean-Baptiste over 4 years ago

The basic thing not being discussed in this lesson is the boolean data type. Boolean is a function common to programming languages where 0 and 1 can be used as false and true respectively. What also isn't clear here (and what I couldn't find on the internet) is what the symbol "++" indicates after \$headCount and \$flipCount in the code. From the function of the program and the structure of the code I want to assume it simply just adds to the already defined variable values. Am I correct?

hain54 over 4 years ago

@Alex, '++' is an increment tool. It is the equivalent of '+1'.

Igor Vilinchuk over 4 years ago

I also was stuck with this question and cleared it up here: http://www.codecademy.com/ru/forum_questions/51550e2a8473cd5d170002b5

0 votes

permalink

simple, rand(0,1) returns either 0 or 1. 0 is interpreted as false, and 1 is a true value. it's all about boolean values! Then the if statement is written in a simple though confusing way (sometimes for beginning learners), the if(\$flip) is essentially saying if(\$flip === true) do the following . Assuming you know the difference between comparison and assignment operators.