Matlab help requesting user input and random integers

13 vues (au cours des 30 derniers jours)
Chris
Chris le 10 Juin 2013
Modifié(e) : Samayochita le 27 Fév 2025
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.

Réponses (1)

Samayochita
Samayochita le 27 Fév 2025
Modifié(e) : Samayochita le 27 Fév 2025
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.

Catégories

En savoir plus sur Random Number Generation dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by