Issue with random function

Hello guys,
I have an issue with the function RANDOM that is not producing random numbers as it should. Here is the code:
for j=1:25
random(j, 1)=rand*(time(j, 1)/2);
random(j, 2)=rand*(time(j, 1)/2);
random(j, 3)=rand*(time(j, 1)/2);
end
If I input this in the command window and assign a value to "time", it works, but in my script, I am always getting the same number on every column! I tried to debug it with breakpoints, but I cannot see why from one line to the other, the "rand" function cannot produce random numbers.
Also inside this for loop, I then have different assignments to the time matrix, eg: time(j, 2) or time(j, 3) so it is not ok to just generate random numbers. I have to connect them with specific entries of the time matrix.

 Accepted Answer

Matt
Matt on 21 Sep 2015
I found the issue to the problem. I named a variable "rand" earlier in my code. Matlab didn't give me any information this would in fact replace the function RAND by the value of the variable rand I declared, so effectively the RAND function was not producing random numbers anymore!
I didn't know using a variable called rand would overwrite Matlab's RAND function. Good to know, problem solved! :-)

3 Comments

Yes, in matlab any variable will take precedence over a function of the same name. Don't use built-in functions names ( sum, mean, i, j are common problems on this forum) for your variables.
A simple way to avoid this problem is to use more descriptive variable names. rand does not tell me much about the purpose of the variable, whereas randompersonindex does (if you're selecting indices in a person list).
Yes. I remember reading something similar to "this is a variable allowed to matlab, it may cause issues to use it", but I cannot remember in what condition.
I just assumed it would not overwrite the function, as I implicitly never used variable names such as sum or mean.
Stephen23
Stephen23 on 21 Sep 2015
Edited: Stephen23 on 21 Sep 2015
Rather than doing this in a loop, it would be faster and more efficient to generate the random numbers all at once. This avoids having to expand the output array in a loop, which is very inefficient.
>> X(:,1) = 0:24;
>> Y = bsxfun(@times,X/2,rand(25,3));

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 21 Sep 2015
What is the class of the variable random? Is it a double at the command line but something else in your script?

2 Comments

I have declared it as being zeros right before the for loop:
random=zeros(25, 3)
I must add that when I put the command RAND right after the start of the for loop, and then just RAND again before the end of the loop, I get the same number! Why is not not generating random numbers all the time? Strange.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 21 Sep 2015

Edited:

on 21 Sep 2015

Community Treasure Hunt

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

Start Hunting!