Matlab help requesting user input and random integers

10 visualizzazioni (ultimi 30 giorni)
Chris
Chris il 10 Giu 2013
Modificato: Samayochita il 27 Feb 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.

Risposte (1)

Samayochita
Samayochita il 27 Feb 2025
Modificato: Samayochita il 27 Feb 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.

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by