Matlab help requesting user input and random integers

9 views (last 30 days)
Chris
Chris on 10 Jun 2013
Edited: Samayochita on 27 Feb 2025 at 8:44
Write a program that will prompt the user for minimum and maximum integers, and then another integer that is the user’s choice in the range from the minimum to the maximum. The script will then generate random integers in the range from the minimum to the maximum, until a match for the user’s choice is generated. The script will print how many random integers had to be generated until a match for the user’s choice was found.

Answers (1)

Samayochita
Samayochita on 27 Feb 2025 at 8:44
Edited: Samayochita on 27 Feb 2025 at 8:44
Hi Chris,
I understand that you are trying to write MATLAB code that prompts the user for minimum and maximum integers, then asks for a target number within that range. It should then generate random numbers in that range until it matches the target and display how many attempts were needed to generate this number.
You can use “rng('shuffle');” to initialize the random number generator using the current time as a seed.
Additionally, you can use “randi” function
(https://www.mathworks.com/help/matlab/ref/randi.html) to generate a random number within the given range.
Keep generating numbers in a loop until a match is found and count how many numbers were generated before finding the match and display the result.
Hope this helps.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!