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

2- It will be important to understand how random numbers are generated using the

ID: 3723902 • Letter: 2

Question

2- It will be important to understand how random numbers are generated using the rand() function. You will also need to have a basic understanding of a while or for loop (look for file "Ch08_LogicalFunctionsTotal "in D2L->contents->Matlab Presentation and HW You are to write a program in which the user tries to guess which number (from 1 to 7, inclusive) that the program generates, based on the rand() function. The first the program asks the user to guess which number will be generated, and if the user is correct, it prints"Good guess!" and if the user is incorrect, it prints "Sorry, try again!" Some other specifications: 1. The program should repeatedly ask the user for a guess, and repeatedly generates a new random number between 1 and 7, and outputs "Number picked: "followed by the number, and the statements as to whether it was correct or not. 2. 2. If the user enters a number of 0 or less, the program should print "Goodbye" and exit. 3. If the user enters a number greater than 7, the program should output "Number too high, guess again!" and then ask for another number. In this last case, no new random number should be generated. Below is a sample output from an execution of the program. In the sample output examples that follow the user input appears after the colon character () and is followed by the enter key Guess a number from 1to 7 (-0 to quit):5 Number picked: 2 Sorry, try again! Guess a number from 1 to 7 (-0 to quit): 7 Number picked: 7 Good guess! Guess a number from 1to 7 (-0 to quit):4 Number picked: 4 Good guess! Guess a number from 1 to 7 (

Explanation / Answer

while 1
    g = input(" Guess a number between 1 and 7 (<=0 to quit):");
    if (g < 1)
        print("Goodbye");
        break;
    else if (g > 7)
        print("Number too high, guess again!");
        continue;
    end
    r = randi([1 7]);
    print("Number picked : ", r);
    if (r == g)
        print("Good guess!");
    else
        print("Sorry, try again!");
    end
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote