2014년 1월 28일 화요일

Unweighted Lotto generating program in Matlab

%% Lotto Number Generation Program (6/45)


%%%
%%%   1st: 1/8,145,060. 2nd: 1/1,357,510. 3rd: 1/34,808. 4th: 1/733.
%%%   5th: 1/44.562
%%%  



clear all;

DATA_MAXNUM = 10000; %% Number of lotto game


result_data = [0 0 0 0 0 0 0]; % result data initialize
cnt = 1;                       % counter


block_lotto = zeros(DATA_MAXNUM, 7);

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

        % random number generating method 1
        %lottonum = floor(rand() * 45 + 1); % 1~45중 하나를 임의로 추출

        % random number generating method 2
        randomA = randperm(45);
        lottonum = randomA(1); % 6 numbers drawn randomly from [1 45]

        % 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 lotto number.
    block_lotto(x, 1:6) = sort(block_lotto(x, 1:6));
   
    %%% to generate 7th number .
    %
    randomA = randperm(45);
    lottonum = randomA(1); % 6 numbers drawn randomly from [1 45]
    result_data(1, 7) = lottonum;
    block_lotto(x, 7) = result_data(1, 7);
   
    %
    if isequal(lottonum == result_data, zeros(1,7))
        result_data(1, 7) = lottonum;
        block_lotto(x, 7) = result_data(1, 7);      
    end
end

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

save_data = uint8(block_lotto);

fid = fopen('D:\program_lang\matlab\lotto\data\578_1_future_UNweight.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);

disp('done...');





%%%  compare program (6/45)
%%%  save this program another file .


clear all;


%
%%  579th lotto number
real_lotto_data = [5, 7, 20, 22, 37, 42, 39]; %


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

correct_selection = zeros(2, row); %


%%%
%%%    
%%%

%
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('UNweight grade : ');
disp(grade);



2014년 1월 27일 월요일

Math special constants

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);

2014년 1월 24일 금요일

Merkle tree structure of transaction in bitcoin




https://en.bitcoin.it/wiki/Protocol_specification#block



Merkle tree is to make sure that data blocks (transactions) received
from other peers in a peer-to-peer network are received undamaged and unaltered,
and even to check that the other peers do not lie and send fake blocks.
Merkle tree can be used to verify any kind of data stored.

proof of work of bitcoin : satoshi paper


1.2) Proof-of-Work of bitcoin
    1). Scanning for a value that when hashed, the hash begins with number of zero bits.
   
    2). increment a nonce in the block until a value is found,  that gives the blocks hash the required zero bits

    3). As later blocks are chained after it,

          * the work to change the block would include redoing all the blocks  after it.
 
        * Every block contains a hash of the previous block.

             - Creating a chain of blocks from the genesis block to the current block.

            - By these properties, double-spending of bitcoins very difficult.


    4) Proof-of-work difficulty is determined by a moving average targeting an average number of blocks per hour.
    

v* Problem of majority decision in bitcoin
    
     1) Proof-of work is one-CPU-one-vote, not one-IP-one-vote.

     2) Majority decision is represented by the longest chain.

         è To modify a past block, an attacker redo the proof-of-work of the block and all blocks after it.

         è Probability of attack reduces exponentially as next blocks are added.
 



* Block of bitcoin 

  
  * There are multiple valid solutions for any given block

    - only one of the solutions needs to be found for the block to be solved.
  * Coinbase transaction:  known as a generation transaction.

      - appearing the first transaction in every block.
  * The number of generated Bitcoins per block: starts at 50.

      - halved every 210,000 blocks (about four years).
  * Bitcoin transactions are broadcast to the network,

      - all peers trying to solve blocks collect the transaction records.

      - add the transaction records to the block.
  * The difficulty of bitcoin is automatically adjusted by the network,

      - solving an average of 6 blocks per hour.

      - every 2016 blocks (about two weeks).
  * This increases (or decreases) the difficulty of generating blocks.


  * Hash of block:  SHA-256 hash of a block's header.

     - between 0the maximum value of a 256-bit number.

     - bitcoin uses: SHA256(SHA256(Block_Header))
  * If your hash is below the target, then you win.

     - If not, you increment the nonce and try again.

transaction of bitcoin : satoshi paper


Bitcoin: A Peer-to-Peer Electronic Cash System   by  Satoshi Nakamoto.

1) Transaction

     1). Electronic coin : define as chain of digital signature.

     2). Transfer : adding the end of the coin with

            - signing   1) a hash of the previous transaction.

                2) public key of the next owner.

           - buyer : verify the signature.

Transaction data
   1. Input.

      * Previous tx : a hash of a previous transaction.

       * ScriptSig : the first half of a script.

           - hash of a simplified version of the transaction.

             - cf). Script : a signature + a public key.

  2. Output.

      * ScriptPubKey is the second half of a script.

            - The public key : buyer.
  3. verification.
     * ScriptSig + ScriptPubKey  : prove the transaction was created by the real owner.





Form of transaction data


https://en.bitcoin.it/wiki/Transactions