⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lottery.m

📁 其中提到遺傳學的程式碼與應用提供給次淚相向的研究者參考下載
💻 M
字号:
function [NUMBERS, check]=lottery(times,GUESS,TAIL,WINNER)
% function NUMBERS=lottery(times,GUESS,TAIL,WINNER)
% CALLING example:
%    lottery(30,[7 17 27 37 3 5 8 9],[0 2 4],[12 14 15 24 36 39 9],TTmean,TTstd);
%    OLD=lottery(30,[7 17 27 37 3 5 8 9],TTmean,TTstd);
% Given a bag of BALLS(GUESS), pour out times of 6 balls for you.

% PenChen Chou. 4/12/2002

reward=0;cost=0;
% Check times
if length(times)~=1, 
    error('Wrong! You need to input times first'); 
end
% Check WINNER number existed or not
if nargin==2, WINNER=[]; TAIL=[]; end
% Enough numbers to be selected
if length(GUESS)<=6
    error('ERROR==>No enough numbers (>=7) to be selected.');
end

% Iterations and generates NUMBER
NUMBERS=[]; N_balls=length(GUESS); 
% Make sure no repeat numbers in GUESS.
%GUESS=chk_rep(GUESS);
% Randomize
GUESS=GUESS(randperm(N_balls));
ITER=0;
while ITER<times
    GOT=ones(1,N_balls); % BALLS are ready.
    NUMBER=[]; 
    for j=1:6
      x=fix((N_balls-0.001)*rand)+1;
      while (GOT(x)~=1)
         x=fix((N_balls-0.001)*rand)+1;
      end
      GOT(x)=0; NUMBER=[NUMBER GUESS(x)];
    end
    % Sort it
    NUMBER=sort(NUMBER);  %UP TO NOW, IT IS OKAY.
    
% Check mean and std to skip those outside ranges
    mx=mean(NUMBER);sx=std(NUMBER);
    % The following is estimated from winners.m file.
    %if (mx>=12 & mx<=31) & (sx>=5 & sx<=17)
    if (mx>=9 & mx<=35) & (sx>=1 & sx<=27)
       % ITER=ITER+1;
    else
       % NUMBER=[];
    end
    
    % Check end of NUMBER to contain 9.
    if ~isempty(NUMBER)
       resid=rem(NUMBER,10); I=find(resid==9);
       if isempty(I)
         %  NUMBER=[];
       end
    end
    % Check NUMBER can be divided by 5 or skip it.
    if ~isempty(NUMBER)
       resid=rem(NUMBER, 5); I=find(resid==0);
       if isempty(I)
        %   NUMBER=[];
       end
    end
    
    % CHECK tails
    %if ~isempty(TAIL) & ~isempty(NUMBER)
    %    Nt=length(TAIL);
    %    for Ni=1:Nt
    %        NUMBER=chk_tail(NUMBER,TAIL(Ni));
    %        if isempty(NUMBER)
    %            break;
    %        end
    %     end
    %end

    % Finally, we get the outcoming numbers or not
    NUMBERS=[NUMBERS; NUMBER];
    if ~isempty(NUMBER), ITER=ITER+1; end
end % end of for

% Delete those repeated numbers
[M,N]=size(NUMBERS);
for i=1:M-1
    for j=i+1:M
        if sum(abs(NUMBERS(i,:)-NUMBERS(j,:)))==0
            NUMBERS(j,:)=[0 0 0 0 0 0];
            %fprintf('Repeat once\n');
        end
    end
end
I=find(NUMBERS(:,1)==0);
if ~isempty(I)
    NUMBERS(I,:)=[];
end

% Select the most 7 appearant numbers out of NUMBERS
APPEAR=0*ones(1,42);
NUMM=NUMBERS(:);
[M]=length(NUMM);
for i=1:M
    for j=1:42
      if (j==NUMM(i))
        APPEAR(j)=APPEAR(j)+1;
      end
    end
end
BINGO=[1:42;APPEAR], countb=0; MOST=[];
while (countb<7)
   [Y I]=max(BINGO(2,:));
   MOST=[MOST BINGO(1,I(1))]; BINGO(:,I(1))=[];
   countb=countb+1;
end
N1=sort(MOST(2:7));
N2=sort([MOST(1) MOST(3:7)]);
N3=sort([MOST(1:2) MOST(4:7)]);
N4=sort([MOST(1:3) MOST(5:7)]);
N5=sort([MOST(1:4) MOST(6:7)]);
N6=sort([MOST(1:5) MOST(7)]);
N7=sort([MOST(1:6)]);

% Out of range check
% 1st digit: 1--20
% 2nd digit: 3--25
% 3rd digit: 7--30
% 4th digit:11--37
% 5th digit:16--39
% 6th digit:28--42
xx3e=0;
if xx3e
RANGE=[1  3  7 11 16 28
      20 25 30 37 39 42];
RANGE=RANGE';
[M, N]=size(NUMBERS);
for i=1:M
    if NUMBERS(i,1)>RANGE(1,2), 
        NUMBERS(i,:)=[0 0 0 0 0 0];
    end
    if NUMBERS(i,2)>RANGE(2,2) & NUMBERS(i,2)<RANGE(2,1), 
        NUMBERS(i,:)=[0 0 0 0 0 0];
    end
    if NUMBERS(i,3)>RANGE(3,2) & NUMBERS(i,3)<RANGE(3,1), 
        NUMBERS(i,:)=[0 0 0 0 0 0];
    end
    if NUMBERS(i,4)>RANGE(4,2) & NUMBERS(i,4)<RANGE(4,1), 
        NUMBERS(i,:)=[0 0 0 0 0 0];
    end
    if NUMBERS(i,5)>RANGE(5,2) & NUMBERS(i,5)<RANGE(6,1), 
        NUMBERS(i,:)=[0 0 0 0 0 0];
    end
    if NUMBERS(i,6)<RANGE(6,1), 
        NUMBERS(i,:)=[0 0 0 0 0 0];
    end
end
% Remove them
I=find(NUMBERS(:,1)==0);
if ~isempty(I)
    NUMBERS(I,:)=[];
end
end

% Remove those outside ranges of mean and std
[M, N]=size(NUMBERS);
for H=1:M
    Hm=mean(NUMBERS(H,:)); Hs=std(NUMBERS(H,:));
    if (Hm>40 | Hm<1 | Hs>20 | Hs<3)
        NUMBERS(H,:)=[0 0 0 0 0 0];
    end
end
% Remove them
I=find(NUMBERS(:,1)==0);
if ~isempty(I)
    NUMBERS(I,:)=[];
end

% Remove those less than 2 of 3-multiply, no 5-multiply and no 9 ending.
No_OK=1;
if (No_OK==0)
[M, N]=size(NUMBERS); L3=0; L5=0; L9=0;
for H=1:M
    x=rem(NUMBERS(H,:),3);
    I=find(x==0); if ~isempty(I), L3=length(I); end;
    x=rem(NUMBERS(H,:),5);
    I=find(x==0); if ~isempty(I), L5=length(I); end;
    x=rem(NUMBERS(H,:),10);
    I=find(x==9); if ~isempty(I), L9=length(I); end;
    if ~(L3>=2 | (L5+L9)>=3)
        NUMBERS(H,:)=[0 0 0 0 0 0];
    end
end
% Remove them
I=find(NUMBERS(:,1)==0);
if ~isempty(I)
    NUMBERS(I,:)=[];
end
end

% Check special code
xv3=0;
if xv3
[M N]=size(NUMBERS);
for i=1:M
    NUMBER=sort(NUMBERS(i,:));
    kkk=[5 6 9 NUMBER(4:5) 39];
    if sum(abs((kkk-NUMBER)))==0
        NUMBER,fprintf('GOT one!\n');pause
    else
       % fprintf('Got NONE\n');
    end
end
end

% NUMBERS are ready soon
%NUMBERS=[NUMBERS;[0 0 0 0 0 0];N1;N2;N3;N4;N5;N6;N7;];
%NUMBERS=[NUMBERS;N1;N2;N3;N4;N5;N6;N7;];

% Load old data
LOAD_old=input('===>Load OLD data(in OLD.mat, var is OLD)? <CR>=NO: ');
if ~isempty(LOAD_old)
    load OLD
    if ~isempty(OLD)
       NUMBERS=OLD;
    end
else
    fprintf('NOTE: New generated NUMBERS is saved in OLD.mat!\n');
    OLD=NUMBERS; save OLD OLD
end

% Check winners
[M,N]=size(NUMBERS);
if ~isempty(WINNER)
    WINNERx=WINNER;SPECIAL=WINNER(7);WINNER(7)=[];
    check=0*ones(M,1);
    % Check EXTRA
    WINNER1=sort([SPECIAL WINNER(2:6)]);
    for i=1:M
        if WINNER1==NUMBERS(i,:), check(i)=7; end;
    end
    
    WINNER2=sort([SPECIAL WINNER(1) WINNER(3:6)]);
    for i=1:M
        if WINNER2==NUMBERS(i,:), check(i)=7; end;
   end; %check'
    
    WINNER3=sort([SPECIAL WINNER(1:2) WINNER(4:6)]);
    for i=1:M
        if WINNER3==NUMBERS(i,:), check(i)=7; end;
    end; %check'
    
    WINNER4=sort([SPECIAL WINNER(1:3) WINNER(5:6)]);
    for i=1:M
        if WINNER4==NUMBERS(i,:), check(i)=7; end;
    end; %check'
    
    WINNER5=sort([SPECIAL WINNER(1:4) WINNER(6)]);
    for i=1:M
        if WINNER5==NUMBERS(i,:), check(i)=7; end;
    end
    
    WINNER6=sort([SPECIAL WINNER(1:5)]);
    for i=1:M
        if WINNER6==NUMBERS(i,:), check(i)=7; end;
    end; %check'
    
    % Check regular reward
    if ~isempty(find(check~=7))
      for i=1:M
        hey=check_in(0,WINNER,NUMBERS(i,:));
        if hey>check(i)
            check(i)=hey;
        end
      end
    end
end
% Print out MEAN and STD
LOTTO=NUMBERS;
mea=mean(LOTTO')';
st= std( LOTTO')';
meh=mean(LOTTO);
LOTTO=[LOTTO mea st];
LOTTO=[LOTTO;meh 0 0]
% Print out in a smart way
seq=1:M;seq=seq';
[M,N]=size(NUMBERS);

for i=1:M
  if check(i)>=3
    for j=1:6
        if NUMBERS(i,j)<=9 & NUMBERS(i,j)>=1
            fprintf('0%-2i',NUMBERS(i,j));
        else
            fprintf('%-3i',NUMBERS(i,j));
        end
        % Print check
        if j==6
           fprintf(' (%d)',check(i));
        end
    end;
    fprintf('\n');
  end  
end

% Check rewards
cost=50*M;
if nargin<=2
    WINNER=[];
end
if ~isempty(WINNER)
I=find(check==3);
if ~isempty(I)
    reward=200*length(I);
end
I=find(check==4);
if ~isempty(I)
    reward=reward+2500*length(I);
end
I=find(check==5);
if ~isempty(I)
    reward=reward+80000*length(I);
end
I=find(check==6);
if ~isempty(I)
    reward=reward+10000000*length(I);
end
I=find(check==7);
if ~isempty(I)
    reward=reward+2000000*length(I);
end

% Print out winner
if ~isempty(WINNERx)
    fprintf('WINNER  = [');
    for i=1:6
        fprintf('%3d',WINNERx(i));
    end
    fprintf(' (%2d)]\n',WINNERx(7));
end
fprintf('Your cost   is  NT$(%5i)!\n',cost);
fprintf('Your reward is  NT$(%5i)!\n',reward);
fprintf('Reward/Cost is %5.2f!\n',reward/cost);
end

[M N]=size(NUMBERS);
fprintf('==>There are %d sets of LOTTO numbers for you! (In LOTTO.mat)\n\n',M);
save LOTTO NUMBERS
%X1=1:M;X1=X1'; 
%if isempty(LOAD_old)
%   NUMBERS=[NUMBERS X1];
%end


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -