How can I repeat this random process 3 times so I can get 3 different versions of the variable "number"?

3 views (last 30 days)
function rollingdice2
i = 1;
modelaparents = 100;
modela= randi(6,1,modelaparents);
modelaparents = length(modela);
number1(1,i) = modelaparents;
while modelaparents > 0;
i = i + 1;
modela= randi(6,1,modelaparents);
modelaparents = length(modela(modela ~= 6));
number1(1,i) = modelaparents;
end

Answers (1)

Jan
Jan on 18 Oct 2015
Edited: Jan on 18 Oct 2015
Your function with an output argument:
function number = rollingdice2
i = 1;
modelaparents = 100;
modela = randi(6,1,modelaparents);
modelaparents = length(modela);
number(1,i) = modelaparents;
while modelaparents > 0;
i = i + 1;
modela= randi(6,1,modelaparents);
modelaparents = length(modela(modela ~= 6));
number(1,i) = modelaparents;
end
calling from your main function:
numberList = cell(1, 3);
for k = 1:3
numberList{k} = rollingdice2;
end
Now you have 3 results as elements of a cell. You've acked for 3 different results. If this detail matters, you have to compare the results with each other and call the function until you have unique results.
  2 Comments
Norman Toro
Norman Toro on 18 Oct 2015
Thank you, I put in the code but it is giving out an empty matrix every time it is supposed to give a different "number" inside the cell. Then after that the program just crashes.
function number = justrolling
i = 1;
modelaparents = 100;
modela = randi(6,1,modelaparents);
modelaparents = length(modela);
number(1,i) = modelaparents;
while modelaparents > 0;
i = i + 1;
modela= randi(6,1,modelaparents);
modelaparents = length(modela(modela ~= 6));
number(1,i) = modelaparents
end
numberList = cell(1, 3)
for k = 1:3
numberList{k} = justrolling
end
end
Walter Roberson
Walter Roberson on 19 Oct 2015
You have an "end" at the bottom that can only match the "function" line. That would, however, imply that your lines starting from "numerList" are part of justrolling.m . If that is the case then each time you invoked justrolling it would get down to the bottom and invoke justrolling again recusively, infinitely.
The code you have starting from "numerList" cannot be in the same function justrolling .

Sign in to comment.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!