Why do the random numbers repeat in each run?

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.

2 Comments

Matt J
Matt J on 25 Mar 2022
Edited: Matt J on 25 Mar 2022
I see that when I run my code...
By now you should know, whatever you say you see, we can not believe unless you demonstrate how we can generate it as well.
Thank you for your comment, I have corrected the question.

Sign in to comment.

 Accepted Answer

When you use randi() each value is determined independently of the others.
For example
HT = 'HT';
HT(randi(2, 1, 5))
ans = 'HTTHT'
If, hypothetically, it was not possible for randi to repeat values, then at most two values could be output and it would either have to be 'HT' or 'TH' .
randperm() on the other hand cannot output duplicates within one call . It is completely permitted to output duplicates across different calls.

6 Comments

Yro
Yro on 25 Mar 2022
Edited: Yro on 25 Mar 2022
Thank you for your clarification. I corrected the question by adding part of my code.
Is the issue that over multiple calls, you are getting the same result more than once for
sample = randsample(FA, 2);
??
If so you need to understand that each permutation is created independently of each other permutation. After that, you need to remember The Birthday Paradox.
FA*(FA-1) is the number of different permutations of selecting 2 elements out of FA different elements. That might be, for example, for FA = 19 it would be 19*18 = 342 different possibilities. Close to 365. Now, if you have a group of people together in a room, how many people do you need in the group before there is a 50% chance that two people in the room have the same birthday (---> two random samples have the same permutation) ? Answer: only about 30. Because in order for there to not be any duplicates, the first would have to be different than the other 29, the second would have to be different than the other 28, the third would have to be different than the other 27... the probability that something matches something goes up much faster than you would expect.
If you need to have different random samples on each iteration, you have a few possibilities:
  • as you go, you could keep a list of the permutations you have already generated, and as you generate each new possibility, test it against the ones you already have, and try again if it matches; OR
  • ask to generate a number of random permutations equal to the number more you need. Discard from that list any entries that you already have, and put the "new" ones on the end of the list. The difference between the total number you need and the number you have already becomes the new number to request to generate next time; you would be storing all of these into a list, and once you have generated them all, you would iterate through them; OR
  • generate a complete list of possibilities ahead of time, and randperm() the number of permutations and the number you need, and then use that vector produced by randperm() to act as row indices into the complete list of possibilties; OR
  • use randperm() to generate a vector of values 1:number of permutations. Then as you go through the iterations, "decode" from the permutation index into the specific permutation content (this does not require storing all possibilities and so would be preferred for the case where the number is large
Yro
Yro on 25 Mar 2022
Edited: Yro on 25 Mar 2022
Thank you again, I will take into account what you explain. Maybe I have not explained well what happens, I do get a "perturbed" pattern with the code, the problem is that I always get the same pattern when I run the code. When I use Octave the pattern in each run is different.
MATLAB initializes with rng('default')
Initializes Mersenne Twister generator with seed 0. This is the default setting at the start of each MATLAB session.
So if you do not change the random number seed, then each MATLAB session would start out with exactly the same pattern of random numbers.
Thank you for your help.
Kind regards.

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Asked:

Yro
on 25 Mar 2022

Commented:

Yro
on 26 Mar 2022

Community Treasure Hunt

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

Start Hunting!