random number generator rng performs differently on compiled binaries (in Linux)
39 views (last 30 days)
Show older comments
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! :-)
0 Comments
Answers (1)
Steven Lord
on 7 Apr 2021
In your application, does the user call your function with the seed value that you pass into rng?
x1 = myfun(42)
x2 = myfun(999)
x3 = myfun(42)
x4 = myfun(1)
x5 = myfun('1') % Not equal to x4 but equal to x6
x6 = myfun(49)
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.]
See Also
Categories
Find more on Structures 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!