Hi! I have a for loop that fills my vector with fibonacci numbers if random number from 0 to 1 is greater than 0,1. In cases when random number is less than 0,1, instead of adding the leading digits, it subtracts. So normally result looks like this:
But in some cases random number from 0 to 1 gives value less than 0,1 twice in a row and i get a table with fibonacci number <= 0 that looks like this:
So i have two questions that i have not managed to solve.
- How to stop the loop when i get first negative value (in this case -2) and write a message about that?
- How to make this for loop automaticly go over and over again (implement it in while loop?) until i get a negative value and than stop it and then write this message? Is it possible to count how many times (while?) loop had to start before negative value was found?
n = 10;
limitf = 0.1;
F = zeros(n,1);
F(1) = 1;
F(2) = 1;
for k = 3:n
r = rand(1);
if r > limitf
F(k) = F(k-1) + F(k-2);
F(k) = F(k-1) - F(k-2);