% 十进制浮点数序列转化为IEEE754标准格式的二进制序列;
% 二进制序列转化为二进制格式输出,以便软件读写;
format long
x0=0.5;
y0=0.2;
a=250;
b=250;
M = 2^(20);
w = zeros(M, 2);
P = char(64, 2);
B = char(16*M, 2);
for k=1:M;
w(k, 1) = x0;
w(k, 2) = y0;
P(1:64, 1) = float2bin(x0);
P(1:64, 2) = float2bin(y0);
B(16*k-15:16*k,1) = P(33:48,1);
B(16*k-15:16*k,2) = P(33:48,2);
x = 1 - a*x0*x0 + y0;
y=b*x0;
x0=x;
y0=y;
end
fid = fopen('CSCM1_PRNGx.bin', 'wb');
for i = 1:length(B(:,1)')
str = B(:,1)';
m = str2num(str(i));
fwrite(fid, m, 'ubit1');
end
fclose(fid);
fid = fopen('CSCM1_PRNGy.bin', 'wb');
for j = 1:length(B(:,2)')
str = B(:,2)';
n = str2num(str(j));
fwrite(fid, n, 'ubit1');
end
fclose(fid);
% 读二进制序列
% file='CSCM1_PRNG1.bin';
% f_id=fopen(file);
% [msg,len_total]=fread(f_id,'ubit1')
% 写txt文件
% fid = fopen('CSCM1(1).txt', 'wt'); %-t模式按照文本而非二进制模式读写
% fprintf(fid,[repmat('%s\t', 1, size(B,2)), '\n'], B(:,1)');
% fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%MATLAB子程序float2bin
function b = float2bin(f)
%%%%%%%%%%%%This function converts a floating point number to a binary string.
%
%Input: f - floating point number, either double or single
%Output: b - string of "0"s and "1"s in IEEE 754 floating point format
%
%Floating Point Binary Formats
%Single: 1 sign bit, 8 exponent bits, 23 significand bits
%Double: 1 sign bit, 11 exponent bits, 52 significand bits
%Input checking
if ~isfloat(f)
disp('Input must be a floating point number.');
return;
end
hex = '0123456789abcdef'; %Hex characters
h = num2hex(f); %Convert from float to hex characters
hc = num2cell(h); %Convert to cell array of chars
nums = cellfun(@(x) find(hex == x) - 1, hc); %Convert to array of numbers
bins = dec2bin(nums, 4); %Convert to array of binary number strings
b = reshape(bins.', 1, numel(bins)); %Reshape into horizontal vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
软件运行程序:
#include "gdef.h"
#include "swrite.h"
#include "bbattery.h"
int main (void)
{
swrite_Basic = FALSE;
bbattery_BlockAlphabitFile("CSCM1_PRNG1.bin", 1048576);
// bbattery_AlphabitFile ("CSCM1_PRNG1.bin", 1048576);
// bbattery_RabbitFile ("CSCM1_PRNG1.bin", 1048576);
return 0;
}
运行.bat软件,定位到文件夹,运行语句gcc my.c -o my.exe -ltestu01 -lprobdist -lmylib -lwsock32
(说明,my.c和my.exe可以自由命名),运行生成的my.exe得出检测结果。