Saving randomly generated sequence into a binary file
1 次查看(过去 30 天)
显示 更早的评论
Hello.
I wrote a simple logistics function that i use for my pesudo random number generator.I want to test it's randomness so i downloaded a custom windows version of NIST STS.
the program rqeuires you to input the location of the binary file (.bin) of your data. i tried this code to save my sequence to .bin file but the program doesn't recognize it.
please help.
the NIST-STS site : https://randomness-tests.fi.muni.cz/
i tried the demo .bin data that came with the software and it worked. only mine is being refused.
the code :
fileID = fopen('Sequence.bin','w');
fwrite(fileID,(PRBG));
fclose(fileID);
0 个评论
回答(1 个)
Arjun
2024-8-26
As per my understanding, you have generated a .BIN file using custom PRNG and want to test randomness of generated data, but your file is not recognized by the testing program.
To address this concern, there can be many reasons behind it but what I suspect is that when writing data to binary file for use with the NIST Statistical Test Suite, it’s very important to verify that the data is correctly formatted.
The NIST STS expects binary data in a specific format, typically as a sequence of bits (0s and 1s). If your pseudo-random number generator (PRNG) produces numbers in a different format (e.g., decimals or integers), you'll need to convert these to a bit sequence before writing them to a file.
Kindly ensure to add the following verification steps in code before writing data into .BIN file and it should resolve the problem you are facing.
Please find the code attached for the same here:
% Example PRNG output (replace this with your actual PRNG output)
PRBG = [1, 0, 1, 1, 0, 0, 1, 0]; % Example bit sequence
% Ensure PRBG is a vector of bits (0s and 1s)
if any(PRBG ~= 0 & PRBG ~= 1)
error('PRBG must be a binary vector containing only 0s and 1s.');
end
% Open a file for writing in binary mode
fileID = fopen('Sequence.bin', 'w');
% Write the bit sequence to the file
fwrite(fileID, PRBG, 'ubit1'); % 'ubit1' writes each bit as a single bit
% Close the file
fclose(fileID);
When you run your code after such a verification either you will get an error message mentioned in the code or a .BIN fill will be generated which can be then used to test the randomness.
Please go through the documentation link of “fwrite” : https://www.mathworks.com/help/releases/R2020a/matlab/ref/fwrite.html?searchHighlight=fwrite&s_tid=doc_srchtitle
I hope this helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!