Why do the random numbers repeat in each run?
Show older comments
Hi, I have the following issue. I see that when I run this function to generate a number of perturbations num_perm to a given array solution.position. This function is part of a more extensive code. The issue is that when I run the function I get a repeated output and it should be random. I add the rng('shuffle') function and I do get different values in each execution. I have used the randi and randsample functions and the behavior is the same. What could be going on, I usually get the random solution without adding any rng options. When I run the same code in Octave I do get random solution.position.
function [solution, f] = INITIAL_PERTURBATION(solution, num_perm, FA)
r = 1;
while (r <= num_perm)
% r1 = randi([1 FA - 1]);
% r2 = randi([2 FA]);
sample = randsample(FA, 2);
r1 = sample(1);
r2 = sample(2);
if ~isequal(r1, r2)
fprintf(' \n Generating initial solution... %d \n ', r)
% Apply permutations to positions and concentrations arrays
new_position = SWAP(solution.position, r1, r2);
new_concentration_array = SWAP(solution.concentration_array, r1, r2);
% Check contiguous fresh fuel assemblies
U238 = 20.53285;
[~, idxcol] = find(new_concentration_array == U238);
fresh_assemblies = idxcol'; % ########
t = CHECK_CONTIGUOUS_ASSEMBLIES(fresh_assemblies);
if t == 1
continue;
else
solution.position = new_position;
solution.concentration_array = new_concentration_array;
f = fresh_assemblies;
r = r + 1;
end
end
end
Thanks in advance. Kind regards.
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!