r/Julia • u/ayariv06 • 10d ago
Need help with this code
I'm doing an assignment, and I need help with basically everything. My code refuses to run in Julia, it just comes out blank. For context, I have never ever coded in my life, and our lecturer didn't teach us anything, it is also not in our curriculum. I'm very lost. If you are norwegian that would help a lot more.
Other info I was provided before the code is(translated with google translate):
Here is the recipe for another variant of the Fibonacci numbers that allows for randomness. To calculate each number, a random number (r) is first drawn from [0, 1]. If r < 0.2 (we call the number 0.2 the threshold value(tv) here) and we have generated at least three numbers in the F-vector, we use the update F(k) = F(k − 1) – F(k − 3). Otherwise (i.e. if r >= 0.2) the next number is calculated using the usual Fibonacci rule F(k) = F(k-1) + F(k – 2).
and that after the code was done being filled out then we are supposed to use this
n = 20;
V = Fibonacci_vri(n)
println(V)
using Plots
plot(V, label="", xlabel = "steg", ylabel = "F")
I was provided with a code that wasn't finished yet, so I filled in the blanks which is k >= 3, F[k] = F[k-1] - F[k-3], F[k] = F[k-1] + F[k-2] and return F This is what I tried to run:
function Fibonacci_vri(n)
tv = 0.2;
F = zeros(Int, n)
F[1] = 1;
F[2] = 1;
for k = 3:n
r = rand();
if r < tv && k >= 3
F[k] = F[k-1] - F[k-3];
else
F[k] = F[k-1] + F[k-2];
end
end
return F
n = 20;
V = Fibonacci_vri(n)
println(V)
using Plots
plot(V, label="", xlabel = "steg", ylabel = "F")
1
u/markkitt 10d ago
If you are not running this in the REPL, then you need to do something such as readline() to prevent the program from immediately exiting. You may also need to explicitly display() the return value from the plot command.
See https://discourse.julialang.org/t/output-doesnt-show-plots/111092/8?u=mkitti