I am writing a Matlab script which requires the repeated generation of random numbers. I am running into an issue where if I pass my random generation function as a parameter to another function, then within the function to which it is passed, it only evaluates once.
Here is some example code:
This is my file randgen.m:
function number = randgen()
'HELLO WORLD'
number = rand(1);
end
Here is my file problemtester.m:
function arr = problemtester(rgen)
firstrand = rgen();
for i = 1:1000
arr(i) = rgen();
end
When I run the command
x = problemtester(randgen);
HELLO WORLD is printed once, and x is filled with 1000 copies of the same random number, so the function must only have evaluated once even though the loop ran 1000 times. Why is this function only evaluating once, despite the repeated calls to it, and more importantly, how do I make it call on each loop iteration?