Saving randomly generated sequence into a binary file

4 次查看(过去 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.
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);

回答(1 个)

Arjun
Arjun about 22 hours 前
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.
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 R Language 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by