Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

TASK(PHP): Use a while-loop to simulate a dice roll (random number between 1-6).

ID: 3589526 • Letter: T

Question

TASK(PHP): Use a while-loop to simulate a dice roll (random number between 1-6). Stop the loop when a 6 is “rolled”.  

Task Description
This is a very simple task to help you understand how to use a while loop. Your task is to simulate rolling a standard dice until a 6 is reached. To do that, create a variable called “$dice” and first assign it -1. Then create a while loop that keeps looping until $dice is equal to 6.    You need to think carefully about what is the appropriate “condition” for this loop. Inside the loop, assign a new random variable to $dice and print out the value (as shown above).  
<!DOCTYPE html>

<html>
<head>
<title> PHP Arrays, Strings, and File </title>
<meta charset="utf-8">
<link rel="stylesheet" href="Lab4.css" type="text/css">
</head>
<body>
<?php


    ini_set('display_errors', 1); # only need to call these functions
    error_reporting(E_ALL);       # one time
?>

<h1> Lab 4 </h1>
<h1> <span> lab </span> </h1>
<hr>

<!-- TASK 1 -- Write your PHP code in in the space provided below.   -->
<div class="task">
    <h2> Task 1: While loop </h2>
      <p> Simulate multiple dice rolls until a 6 is reached. Print out the results of each roll. </p>
<div class="output">
      <?php
        /* Task 1: Put your code here */

?>

Explanation / Answer

$dice = 5;
while($dice != 6) {
$dice = mt_rand(1, 6);
print("rolled $dice <br /> ");
}

It will print rolled and dice value on each line untill 6 is rolled.