In a lottery the player has to select several numbers out of a list. Write a use
ID: 3635084 • Letter: I
Question
In a lottery the player has to select several numbers out of a list. Write a user-defined function that generates a list of n integer numbers that are uniformly distributed between the numbers a and b. All the selected numbers on the list must be different. Use the function to generate a list of 6 numbers from 1 through 49.%%Use the MATLAB rand function. To make sure that all the numbers are different from each other, the numbers are picked one by one. Each number that is picked (by the rand function) is compared with all the numbers that were picked before. If a match is found, the number is not selected, and a new number is picked.%%
Example: lotto(1,49,6) ans =
23 2 40 22 31 39
Need to use matlab to solve this question
Explanation / Answer
Dear,
function [ lottory ] = lotto( a, b, size )
lottory = (size);
i = 1;
while i <= size
found = false;
randomNumber = randi(b);
j = 1;
while j < i
if randomNumber == lottory(j)
found = true;
break;
end
j = j+1;
end
if found == false
lottory(i) = randomNumber;
i = i+1;
end
end
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.