2014년 1월 27일 월요일

weighted Lotto number generator program in Matlab

%%% Lotto number generating program (6/45)
%%%   


clear all;

%tic  % get processing time

%% total number of lotto game 
DATA_MAXNUM = 10000; 

%% read previous lotto data
before_data = xlsread('D:\program_lang\matlab\lotto\data\data_lotto_.xls'); % 
before_data = uint8(before_data);   % double --> int data 

%%

[row, col] = size(before_data);

num_data = zeros(1, 45);
%size(probability_data);

cnt = 1;  % counter

%% 
%%  the number for getting probability.
%%

PROB_NUM_MAX = row;  %% all data select
%PROB_NUM_MAX = 200;  % recent 200 data select


while (cnt <= PROB_NUM_MAX)
    
    for col = 1:7             % 
        num = before_data(cnt, col);  % read  data     
        num_data(num) = num_data(num) + 1;   % num_data is array of lotto number
    end
    
    cnt = cnt + 1;
end

%num_data  %
probability_data = row .\ num_data;
%disp(num_data);
%disp(sort(probability_data));


%%%  get lotto number
%%% 

result_data = [0 0 0 0 0 0 0]; % 

Number = 1:45;             % possible numbers
weighted = probability_data;   % corresponding weights
genNumber = 1;              % how many numbers to generate
col_1 = 1; % counter


%%%
%%%     DATA_MAXNUM is the total number for generating lotto number
%%%     
%%%


x = 1; % first row.

block_lotto = uint8(zeros(DATA_MAXNUM, 7));


for x = 1: DATA_MAXNUM
    
    y = 1; % 
    while y <= 6   %%%  % 

        % weighted 
        lottonum = uint8(Number(sum( bsxfun(@ge, rand(genNumber,1), cumsum(weighted ./ sum(weighted))), 2) + 1 ));

        % reject same number
        if isequal(lottonum == result_data, zeros(1,7)) 
            result_data(1, y) = lottonum;  
            block_lotto(x, y) = result_data(1, y);
            y = y + 1;
        end
    end
    
    % sorting
    block_lotto(x, 1:6) = sort(block_lotto(x, 1:6));
    result_data(1, y) = lottonum;  
    block_lotto(x, y) = result_data(1, y);
        
    % weighted number 
    lottonum = uint8(Number(sum( bsxfun(@ge, rand(genNumber,1), cumsum(weighted ./ sum(weighted))), 2) + 1 ));
        
    if isequal(lottonum == result_data, zeros(1,7)) 
        result_data(1, y) = lottonum;  
        block_lotto(x, y) = result_data(1, y);
    end
end

%disp('block_lotto  : ');
%disp(block_lotto);

%%% 
%%%    save routine
%%%


save_data = uint8(block_lotto);

fid = fopen('D:\program_lang\matlab\lotto\data\578_1_future_weight.txt', 'wt');

for x = 1:DATA_MAXNUM
    fprintf(fid, '%d %d %d %d %d %d %d\n', save_data(x, 1), save_data(x, 2), save_data(x, 3), save_data(x, 4), save_data(x, 5), save_data(x, 6), save_data(x, 7));
end
fclose(fid);

%toc   % .

disp('The num. of real lotto for probability :');
disp(PROB_NUM_MAX);

disp('done...');





%%   (6/45)
%%%   comparing lotto program


clear all;


%%  582th lotto number
real_lotto_data = [2, 12, 14, 33, 40, 41, 25]; % previous real lotto number



before_data = load('D:\program_lang\matlab\lotto\data\578_1_future_weight.txt');
[row, col] = size(before_data);

correct_selection = zeros(2, row); 
%second_selection = 0;   % 

%%%
%%%     
%%%

  
for x = 1:row 
       for y = 1:6    
                for yy = 1:6 
            % 
            if (before_data(x, y) == real_lotto_data(1,yy))
                correct_selection(1,x) = correct_selection(1, x) + 1;
            end    
        end
        
        if (before_data(x, 7) == real_lotto_data(1,7))
            correct_selection(2, x) = 1;
        end
    end
end

%disp('correct_selection : ');
%disp(correct_selection);


%%%
%%%     count grade
%%%

grade = [0, 0 ,0, 0, 0]; % 1st, 2nd, ... , 5th

for x = 1:row 
    if (correct_selection(1, x) == 3)
        grade(1, 5) = grade(1, 5) + 1;
    elseif (correct_selection(1, x) == 4)
        grade(1, 4) = grade(1, 4) + 1;        
    elseif (correct_selection(1, x) == 5 && (correct_selection(2, x) == 0))
        grade(1, 3) = grade(1, 3) + 1;  
    elseif ((correct_selection(1, x) == 5) && (correct_selection(2, x) == 1))
        grade(1, 2) = grade(1, 2) + 1; 
    elseif (correct_selection(1, x) == 6) 
        grade(1, 1) = grade(1, 1) + 1;         
    end   
end

disp('total number ;');
disp(row);

disp('grade : ');
disp(grade);

댓글 1개:

  1. Hello. How can this script be amended to provide (7/35) numbers? I tried this script but was getting repeats from column 6 (from number 18 and onward) and column 7.

    답글삭제