random number generator rng performs differently on compiled binaries (in Linux)

25 visualizaciones (últimos 30 días)
Hello,
I stumbled over some strange behavior when trying to control random numbers to always get the same results.
In my code I need to create random numbers that are always the same, so that my results are always reproducible. When I compiled my code I got reproduclibly different results. That means the compiled binary always performs the same but different to the non-compiled code.
I could trace it down to a random number generation.
My code is to complex to put it in here, but it looks like that:
randSeed = 1;
rng(randSeed,'twister');
inputPoints = repmat([1:2000]',1,3); % some 3D points
function [new_random_points] = findQuasiRandomPoints(inputPoints);
requiredPoints = 30;
% not so random permutation of my points - this gives the same result
% in the compiled binary
inputPoints = double(inputPoints(randperm(size(inputPoints,1)),:));
% choose a not so random point - this gives the same result
% in the compiled binary
seedPoint = inputPoints(round(rand*size(inputPoints,1)),:);
while size(new_random_points,1)<requiredPoints
% create not so random index to change order - HERE I GET DIFFERENT
% RESULTS IN THE COMPILED BINARY
testpoint_number = [randperm(size(inputPoints,1))]';
for p = 1 : numel(testpoint_number)
newTestIndex = testpoint_number(p);
% here comes some testing procedure and some point is chosen
% that fulfills the requirement
if reqFulfilled == 1
new_random_points = inputPoints( testpoint_number(newTestIndex) ,:);
requiredPoints = requiredPoints+1;
end
end
end
end
The the random number generator gives the same result the first times rand and randperm are called, but in the while loop it fails immediately.
I checked it with a simple example and there it works. Most probably like the first to rand/randperm calls in my code.
Am I missing something?
Can I specify rng in a way that it keeps throwing the same "random" numbers?
Thanks for any help! :-)

Respuestas (1)

Steven Lord
Steven Lord el 7 de Abr. de 2021
In your application, does the user call your function with the seed value that you pass into rng?
x1 = myfun(42)
x1 = 1×7
4 10 8 6 2 2 1
x2 = myfun(999)
x2 = 1×7
9 6 2 7 1 4 5
x3 = myfun(42)
x3 = 1×7
4 10 8 6 2 2 1
x4 = myfun(1)
x4 = 1×7
5 8 1 4 2 1 2
x5 = myfun('1') % Not equal to x4 but equal to x6
x5 = 1×7
4 3 10 9 7 6 6
x6 = myfun(49)
x6 = 1×7
4 3 10 9 7 6 6
function x = myfun(n)
rng(double(n), 'twister')
x = randi(10, 1, 7);
end
If so, as stated on this documentation page: "The input arguments you pass to your executable from a system prompt are received as character vector input. Thus, if you expect the data in a different format (for example, double), you must first convert the character vector input to the required format in your MATLAB code. For example, you can use STR2NUM to convert the character vector input to numerical data."
The seeds 1 and '1' are not the same. [Well, I had to "adjust" the input as otherwise rng would have thrown an error because its seed input was not what it could accept.]
  1 comentario
chacoon
chacoon el 8 de Abr. de 2021
Hey Steven,
thanks for your answer. The seed value is definitely always 1 as a double. The user is not able to interfere ;-)
What confuses me most is that the first two calls of random functions in
inputPoints = double(inputPoints(randperm(size(inputPoints,1)),:));
and
seedPoint = inputPoints(round(rand*size(inputPoints,1)),:);
always give the same results. Only in the while loop I get a different result already in the very first loop.
I tried to create a simpler example:
rng(1,'twister')
p11 = int16(round(rand(1,20)*100))
p12 = randperm( int16(round(rand*100)) )
start = 0;
while start<2
p13 = randperm(numel(p12))
start = start+1;
end
p21 = int16(round(rand(1,20)*100))
p22 = randperm( int16(round(rand*100)) )
start = 0;
while start<2
p23 = randperm(numel(p22))
start = start+1;
end
p31 = int16(round(rand(1,20)*100))
p32 = randperm( int16(round(rand*100)) )
start = 0;
while start<2
p33 = randperm(numel(p32))
start = start+1;
end
That always gives the same result in the compiled version compared to the Matlab version.
Is there any way to figure out what conditions may have changed to get a different results after som same results?
As a workaround:
Does anyone have an idea how to simulate a "randperm" with a fixed quasi random number, like e.g. PI?

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by