MATLAB: How to generate random number within a sum limit numberrandom numberrandom number generator Generate 5 random numbers, where their sum should be less/equal to 50. Best Answer while trueA=randi([1 15],5,1);if(sum(A)<=50) break;endenddisp(A)disp(sum(A)) Related SolutionsMATLAB: Summation of Random Numbers I would use a while loop. while summation <= x %add a random integer to the summationend MATLAB: How to make a computer go to a line in mfile for a repeat execution Matlab does not have that kind of implementation. On the other hand, you can do a work around using conditionals, either whiles or ifs.For example, instead ofB=[2 4 6 8];C=[1.5 3 4 5.6]; A = sum(B-C);if A>1 go to say line 3 ? else disp(A)endYou might do: ( I know the example has no sense, but it's to take your lines as an example)A = 10;while A>1 B=[2 4 6 8]; C=[1.5 3 4 5.6]; A = sum(B-C);enddisp(A) Related QuestionDispHow to generates random numbersFinding the number of values bigger than a certain number in an array
Best Answer