function randtool(action,flag);
%RANDTOOL Demonstration of many random number generators.
%   RANDTOOL(ACTION) creates a histogram of random samples from many
%   distributions. This is a demo that displays a histograms of random
%   samples from the distributions in the Statistics Toolbox. 
%
%   Change the parameters of the distribution by typing in a new
%   value or by moving a slider.
%
%   Output the current sample to a variable in the workspace by pressing
%   the output button.
%
%   Change the sample size by typing any positive integer in the
%   sample size edit box.
%
%   Change the distribution type using the popup menu.

%   B.A. Jones 3-22-93
%   Copyright (c) 1993-96 by The MathWorks, Inc.
%   $Revision: 2.4 $  $Date: 1996/03/20 17:47:56 $

if nargin < 1
    action = 'start';
    flag = 0;
else % nargin is one or two
    if nargout == 1 & strcmp(action,'output')
        action = 'start';
        flag = 1;
    end
    if nargin == 1 & ~strcmp(action,'start')
        flag = 0;
    end
end

%On recursive calls get all necessary handles and data.
if ~strcmp(action,'start')   
   
   f = get(gcf,'Userdata');

   p1_field     = f(1);
   p1_hi        = f(2);
   p1_lo        = f(3);
   p1_slider    = f(4);
   p2_field     = f(5);
   p2_hi        = f(6);
   p2_lo        = f(7);
   p2_slider    = f(8);
   p1_text      = f(9);
   p2_text      = f(10);
   samples_field= f(11);
   popup        = f(12);
   p1limits     = f(13);
   p2limits     = f(14);
   rn           = f(15);
   rand_axes    = f(16);
   p3_field     = f(17);
   p3_hi        = f(18);
   p3_lo        = f(19);
   p3_slider    = f(20);
   p3_text      = f(21);

   p1lim = get(p1limits,'Userdata');
   p2lim = get(p2limits,'Userdata');
   distribution = get(popup,'Userdata');
   parameter1 = str2num(get(p1_field,'String'));
   p1high = str2num(get(p1_hi,'String'));
   p1low = str2num(get(p1_lo,'String'));
   parameter2 = str2num(get(p2_field,'String'));
   p2high = str2num(get(p2_hi,'String'));
   p2low = str2num(get(p2_lo,'String'));
   popupvalue = get(popup,'Value');
   samples = str2num(get(samples_field,'String'));
   random_numbers = get(rn,'UserData');
   histogram = get(rand_axes,'userdata');
   parameter3 = str2num(get(p3_field,'String'));
   p3high = str2num(get(p3_hi,'String'));
   p3low = str2num(get(p3_lo,'String'));

end

if strcmp(action,'start'),
fcolor  = [0.5 0.5 0.5];

rand_fig = figure;
set(rand_fig,'Units','Normalized','Color','k','UserData','rand_fig','Backingstore','off');

gray_axes = axes;
set(gray_axes,'XTick',[],'YTick',[],'Units','Normalized','Position',[0 0 1 1]);
set(gray_axes,'Color',fcolor,'UserData','gray_axes');
set(gray_axes,'ButtonDownFcn','randtool(''stray_click'')');

rand_axes = axes;
set(rand_fig,'CurrentAxes',rand_axes);


% Create intial parameter values 
parameter1  =   0; 
parameter2  =   1;
parameter3  =   0.5;
p1high      =   2;
p1low       =  -2;
p2high      =   2;
p2low       = 0.5;
p3high      =   1;
p3low       = 0.1;
p1lim      = [-Inf Inf];
p2lim      = [0 Inf];
samples     = 100;
bins        = 10;

distribution = 'Normal';

% Set positions of graphic objects
axisp   = [.20 .32 .75 .58];

lfieldp = [.25 .08 .10 .05];
lhip    = lfieldp + [.15 .06 0 0];
llop    = lhip + [0 -0.13 0 0];
lslidep = lfieldp + [.11 -0.07 -0.07 0.13];
ltextp  = lfieldp + [0 0.05 0 0];

sfieldp = [.55 .08 .10 .05];
ship    = sfieldp + [.15 .06 0 0];
slop    = ship + [0 -0.13 0 0];
sslidep = sfieldp + [.11 -0.07 -0.07 0.13];
stextp  = sfieldp + [0 0.05 0 0];

samplefp= sfieldp + [.30 0 0 0];
sampletp= samplefp + [0 0.05 0 0];

xfieldp = [.50 .21 .14 .05];
yfieldp = [0.01 .58 .14 .05];

% Set axis limits and data
xrange   = [p1low - 3 * p2high p1high + 3 * p2high];
p1text   = 'Mu';
p2text   = 'Sigma';

% Generate initial set of random numbers.
random_numbers = random(distribution,parameter1,parameter2,samples,1);

% Define graphics objects
p1_field = uicontrol('Style','edit','Position',lfieldp,...
         'Units','normalized','String',num2str(parameter1),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp1field'')');
         
p1_hi = uicontrol('Style','edit','Position',lhip,...
         'Units','normalized','String',num2str(p1high),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp1hi'')');
         
p1_lo=uicontrol('Style','edit','Position',llop,...
         'Units','normalized','String',num2str(p1low),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp1lo'')');

p1_slider = uicontrol('Style','slider','Position',lslidep,...
        'Units','normalized','Value',parameter1,'Max',p1high,'Min',p1low,...
        'Callback','randtool(''setp1slider'')');

p2_field = uicontrol('Style','edit','Position',sfieldp,...
         'Units','normalized','String',num2str(parameter2),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp2field'')');
         
p2_hi=uicontrol('Style','edit','Position',ship,...
         'Units','normalized','String',num2str(p2high),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp2hi'')');

p2_lo=uicontrol('Style','edit','Position',slop,...
         'Units','normalized','String',num2str(p2low),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp2lo'')');

p2_slider = uicontrol('Style','slider','Position',sslidep,...
        'Units','normalized','Value',parameter2,'Max',p2high,'Min',p2low,...
        'Callback','randtool(''setp2slider'')');

p3_field=uicontrol('Visible','off','Style','edit',...
         'Units','normalized','String',num2str(parameter3),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp3field'')');
         
p3_hi=uicontrol('Visible','off','Style','edit',...
         'Units','normalized','String',num2str(2),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp3hi'')');

p3_lo=uicontrol('Visible','off','Style','edit',...
         'Units','normalized','String',num2str(0.1),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setp3lo'')');

p3_slider=uicontrol('Visible','off','Style','slider',...
        'Units','normalized','Value',parameter3,'Max',2,'Min',0.1,...
        'Callback','randtool(''setp3slider'')');
                   
                   

p1_text = uicontrol('Style','text','Position',ltextp,...
        'Units','normalized','BackgroundColor',fcolor,...
        'ForegroundColor','white','String',p1text);
        
p2_text = uicontrol('Style','text','Position',stextp,...
        'Units','normalized','BackgroundColor',fcolor,...
        'ForegroundColor','white','String',p2text);

p3_text = uicontrol('Visible','off','Style','text','Position',stextp,...
        'Units','normalized','BackgroundColor',fcolor,...
        'ForegroundColor','white','String','Lambda');

samples_text = uicontrol('Style','text','Position',sampletp,...
        'Units','normalized','BackgroundColor',fcolor,...
        'ForegroundColor','white','String','Samples');

samples_field = uicontrol('Style','edit','Position',samplefp,...
         'Units','normalized','String',num2str(samples),...
         'BackgroundColor','white',...
         'CallBack','randtool(''setdata'',2)');

resample_button = uicontrol('Style','Pushbutton',...
         'Position',[.01 .15 .13 .05],'Units','normalized',...
         'Callback','randtool(''setdata'',1)','String','Resample');

output_button = uicontrol('Style','Pushbutton',...
         'Position',[.01 .08 .13 .05],'Units','normalized',...
         'Callback','randtool(''output'',2);','String','Output');
       
close_button = uicontrol('Style','Pushbutton','Units','normalized',...
               'Position',[0.01 0.01 0.13 0.05],'Callback','close','String','Close');

yaxis_text = uicontrol('Style','text','Position',yfieldp,...
        'Units','normalized','BackgroundColor',fcolor,...
        'ForegroundColor','white','String','Counts');
      
xaxis_text = uicontrol('Style','text','Position',xfieldp,...
        'Units','normalized','BackgroundColor',fcolor,...
        'ForegroundColor','white','String','Values');

popup=uicontrol('Style','Popup','String',...
'Beta|Binomial|Chisquare|Discrete Uniform|Exponential|F|Gamma|Geometric|Lognormal|Negative Binomial|Noncentral F|Noncentral T|Noncentral Chi-square|Normal|Poisson|Rayleigh|T|Uniform|Weibull',...
        'Position',[.41 .92 .25 .06],'Units','normalized','UserData',distribution,...
        'CallBack','randtool(''changedistribution'')','Value',14);
                   
p1limits = uicontrol('Style','text','Visible','off',...
        'String','p1limits','UserData',p1lim);
      
p2limits = uicontrol('Style','text','Visible','off',...
        'String','p2limits','UserData',p2lim);

rn = uicontrol('Style','text','Visible','off',...
        'String','random_numbers','UserData',random_numbers);

% Create Histogram
minrn = min(random_numbers);
maxrn = max(random_numbers);
crn = random_numbers - minrn + realmin;
intrn = ceil(bins*crn./(maxrn-minrn));
counts = full(sparse(1,intrn,1));
binvec = (1:bins)';
values = minrn + binvec*(maxrn-minrn)./bins;

[xvalues yvalues] = bar(values,counts);
yrange = [0 1.3*max(max(yvalues))];
histogram = plot(xvalues, yvalues,'w-','EraseMode','xor');
set(rand_axes,'XLim',xrange,'YLim',yrange,'Color','k','UserData',histogram);
set(rand_axes,'Position',axisp,'Box','on','DrawMode','fast');

set(rand_fig,'UserData',...
[p1_field p1_hi p1_lo p1_slider p2_field p2_hi p2_lo p2_slider p1_text p2_text...
 samples_field popup p1limits p2limits rn rand_axes p3_field p3_hi p3_lo p3_slider p3_text]);

if flag == 1
    r = random_numbers;
end
  
% End of initialization activities.

elseif strcmp(action,'setdata'),
    set(gcf,'pointer','watch');


   if popupvalue == 1, 
      distribution = 'Beta'; 
      parameter_count = 2;
   elseif popupvalue == 2, 
      distribution = 'Binomial'; 
      parameter_count = 2;
   elseif popupvalue == 3, 
      distribution = 'Chisquare'; 
      parameter_count = 1;
   elseif popupvalue == 4, 
      distribution = 'Discrete Uniform'; 
      parameter_count = 1;
   elseif popupvalue == 5, 
      distribution = 'Exponential'; 
      parameter_count = 1;
   elseif popupvalue == 6, 
      distribution = 'F'; 
      parameter_count = 2;
   elseif popupvalue == 7, 
      distribution = 'Gamma'; 
      parameter_count = 2;
   elseif popupvalue == 8, 
      distribution = 'Geometric'; 
      parameter_count = 1;
   elseif popupvalue == 9, 
      distribution = 'Lognormal'; 
      parameter_count = 2;
   elseif popupvalue == 10, 
      distribution = 'Negative Binomial'; 
      parameter_count = 2;
   elseif popupvalue == 11, 
      distribution = 'Noncentral F'; 
      parameter_count = 3;
   elseif popupvalue == 12, 
      distribution = 'Noncentral T'; 
      parameter_count = 2;
   elseif popupvalue == 13, 
      distribution = 'Noncentral Chi-square'; 
      parameter_count = 2;
   elseif popupvalue == 14, 
      distribution = 'Normal'; 
      parameter_count = 2;
   elseif popupvalue == 15, 
      distribution = 'Poisson'; 
      parameter_count = 1; 
   elseif popupvalue == 16, 
      distribution = 'Rayleigh'; 
      parameter_count = 1; 
   elseif popupvalue == 17, 
      distribution =   'T'; 
      parameter_count = 1;
   elseif popupvalue == 18, 
      distribution =   'Uniform'; 
      parameter_count = 2;
   elseif popupvalue == 19, 
      distribution =   'Weibull'; 
      parameter_count = 2;
    end

   if flag == 2
       if (floor(samples) ~= samples),
           samples = floor(samples);
           set(samples_field,'String',num2str(samples));
       end
   end

   if flag == 1 | flag == 2
      if parameter_count == 1
           random_numbers = random(distribution,parameter1,samples,1);
      elseif parameter_count == 2
           random_numbers = random(distribution,parameter1,parameter2,samples,1);
      else
           random_numbers = random(distribution,parameter1,parameter2,parameter3,samples,1);
      end
      set(rn,'UserData',random_numbers);
   end
   
   if  popupvalue == 2 | popupvalue == 4 | popupvalue == 8 | ...
        popupvalue == 10 | popupvalue == 15
        values = [0:max(max(random_numbers))];
        counts = full(sparse(1,random_numbers+1,1));
   else
        bins = ceil(sqrt(samples));
        minrn = min(random_numbers);
        maxrn = max(random_numbers);
        crn = random_numbers - minrn + realmin;
        intrn = ceil(bins*crn./(maxrn-minrn));
        k = find(intrn > bins);
        if any(k)
           intrn(k) = bins(ones(size(k)));
        end
        counts = full(sparse(1,intrn,1));
        binvec = (1:bins)';
        values = minrn + binvec*(maxrn-minrn)./bins;
   end

    ylimits = get(gca,'YLim');
    xlimits = get(gca,'XLim');
    

    [xvalues, yvalues] = bar(values(:),counts(:));

    maxy = max(max(yvalues));
    maxx = max(max(xvalues));
    minx = min(min(xvalues));


    if ylimits(2) < maxy  | ylimits(2) > 4*maxy
        set(gca,'YLim',[0 1.1*maxy]);
    end

    if xlimits(2) < maxx 
        set(gca,'XLim',[xlimits(1) maxx]);
    end

    if xlimits(1) > minx
        set(gca,'XLim',[minx xlimits(2)]);
    end
   
    set(histogram,'XData',xvalues,'YData',yvalues);
   
    set(gcf,'pointer','arrow');

elseif strcmp(action,'setp1slider'),
    cv = get(p1_slider,'Value');
    if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
     & popupvalue ~= 6 &  popupvalue ~= 10 & popupvalue ~= 11 ...
     & popupvalue ~= 12 &  popupvalue ~= 13 & popupvalue ~= 17
        parameter1 = cv;
    else
        parameter1 = round(cv);
    end
    set(p1_field,'String',num2str(parameter1));
    randtool('setdata',1);
    
elseif strcmp(action,'setp1field'),
    cv = str2num(get(p1_field,'String'));
    if cv > p1lim(1) & cv < p1lim(2),
        if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
        & popupvalue ~= 6 &  popupvalue ~= 10 & popupvalue ~= 11 ...
        & popupvalue ~= 12 &  popupvalue ~= 13 & popupvalue ~= 17
            parameter1 = cv;
        else
            parameter1 = round(cv);
            set(p1_field,'String',num2str(parameter1));
        end
        if (parameter1 > p1high), 
            set(p1_hi,'String',num2str(parameter1));
            p1high = parameter1;
            set(p1_slider,'Max',p1high);
        end;
        if (parameter1 < p1low), 
            set(p1_lo,'String',num2str(parameter1));
            p1low=parameter1;
            set(p1_slider,'Min',p1low);
        end;
        set(p1_slider,'Value',parameter1);
        randtool('setdata',1);
    else
        set(p1_field,'String',num2str(parameter1));
    end

elseif strcmp(action,'setp1hi'),
    cv = str2num(get(p1_hi,'String'));
    if cv > p1lim(1) & cv < p1lim(2),
        if cv > parameter1,
            if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
            & popupvalue ~= 6 &  popupvalue ~= 10 & popupvalue ~= 11 ...
            & popupvalue ~= 12 &  popupvalue ~= 13 & popupvalue ~= 17
                p1high = cv;
            else
                p1high = round(cv);
                set(p1_hi,'String',num2str(p1high));
            end
            set(p1_slider,'Max',p1high);
        else
            if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
            & popupvalue ~= 6 &  popupvalue ~= 10 & popupvalue ~= 11 ...
            & popupvalue ~= 12 &  popupvalue ~= 13 & popupvalue ~= 17
                set(p1_hi,'String',num2str(p1high));
            else
                set(p1_hi,'String',num2str(round(p1high)));
            end
        end
    else
        set(p1_hi,'String',num2str(p1high));
    end
           
elseif strcmp(action,'setp1lo'),
    cv = str2num(get(p1_lo,'String'));
    if cv > p1lim(1) & cv < p1lim(2),
        if cv < parameter1,
            if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
            & popupvalue ~= 6 &  popupvalue ~= 10 & popupvalue ~= 11 ...
            & popupvalue ~= 12 &  popupvalue ~= 13 & popupvalue ~= 17
                p1low = cv;
            else
                p1low = round(cv);
                set(p1_lo,'String',num2str(p1low))
            end
            set(p1_slider,'Min',p1low);
        else 
            if popupvalue ~= 2 & popupvalue ~= 3 & popupvalue ~= 4 ...
            & popupvalue ~= 6 &  popupvalue ~= 10 & popupvalue ~= 11 ...
            & popupvalue ~= 12 &  popupvalue ~= 13 & popupvalue ~= 17
                set(p1_lo,'String',num2str(p1low));
            else
                set(p1_lo,'String',num2str(round(p1low)));
            end
            set(p1_lo,'String',num2str(p1low));
        end
    else
        set(p1_lo,'String',num2str(p1low));
    end

elseif strcmp(action,'setp2slider'),
    cv = get(p2_slider,'Value');
    if popupvalue ~= 6 & popupvalue ~= 11
        parameter2 = cv;
    else
        parameter2 = round(cv);
    end
    set(p2_field,'String',num2str(parameter2));
    randtool('setdata',1);

elseif strcmp(action,'setp2field'),
    cv = str2num(get(p2_field,'String'));
    if cv > p2lim(1) & cv < p2lim(2),
        if popupvalue ~= 6 & popupvalue ~= 11
            parameter2 = cv;
        else
            parameter2 = round(cv);
            set(p2_field,'String',num2str(parameter2));
        end
        if (parameter2 > p2high),
            p2high = parameter2;
            set(p2_hi,'String',num2str(parameter2));
            set(p2_slider,'Max',p2high);    
            randtool('setdata',1);   
        end;
        if (parameter2 < p2low),
            p2low = parameter2; 
            set(p2_lo,'String',num2str(parameter2),'Min',p2low);
            set(p2_slider,'Min',p2low);
            randtool('setdata',1);   
        end
        set(p2_slider,'Value',parameter2);
        randtool('setdata',1);
    else 
        set(p2_field,'String',num2str(parameter2));
    end

elseif strcmp(action,'setp2hi'),
    cv = str2num(get(p2_hi,'String'));
    if cv > p2lim(1) & cv < p2lim(2),
        if cv > parameter2, 
            if popupvalue ~= 6 & popupvalue ~= 11
                p2high = cv;
            else
                p2high = round(cv);
                set(p2_hi,'String',num2str(p2high));
            end
            set(p2_slider,'Max',p2high);
        else 
            set(p2_hi,'String',num2str(p2high));
        end
    else
        set(p2_hi,'String',num2str(p2high));
    end 

elseif strcmp(action,'setp2lo'),
    cv = str2num(get(p2_lo,'String'));
    if cv > p2lim(1) & cv < p2lim(2),
        if cv < parameter2 
            if popupvalue ~= 6 & popupvalue ~= 11
                p2low = cv;
            else
                p2low = round(cv);
                set(p2_lo,'String',num2str(p2low));
            end
            set(p2_slider,'Min',p2low);
        else 
            set(p2_lo,'String',num2str(p2low));
        end
    else
        set(p2_lo,'String',num2str(p2low)); 
    end
% Set third parameter for Noncentral F only
elseif strcmp(action,'setp3slider'),
    cv = get(p3_slider,'Value');
    parameter3 = cv;
    set(p3_field,'String',num2str(parameter3));
    randtool('setdata',1);
elseif strcmp(action,'setp3field'),
    cv = str2num(get(p3_field,'String'));
    if cv > 0,
        parameter3 = cv;
        if (parameter3 > p3high),
            p3high = parameter3;
            set(p3_hi,'String',num2str(parameter3));
            set(p3_slider,'Max',p3high);    
            randtool('setdata',1);   
        end;
        if (parameter3 < p3low),
            p3low = parameter3; 
            set(p3_lo,'String',num2str(parameter3),'Min',p3low);
            set(p3_slider,'Min',p3low);
            randtool('setdata',1);   
        end
            set(p3_slider,'Value',parameter3);
            randtool('setdata',1);
    else 
        set(p3_field,'String',num2str(parameter3));
    end

elseif strcmp(action,'setp3hi'),
    cv = str2num(get(p3_hi,'String'));
    if cv > 0,
        if cv > parameter3, 
            p3high = cv;
            set(p3_slider,'Max',p3high);
        else 
            set(p3_hi,'String',num2str(p3high));
        end
    else
        set(p3_hi,'String',num2str(p3high));
    end 

elseif strcmp(action,'setp3lo'),
    cv = str2num(get(p3_lo,'String'));
    if cv > 0,
        if cv < parameter3 
            p3low = cv;
            set(p3_slider,'Min',p3low);
        else 
            set(p3_lo,'String',num2str(p3low));
        end
    else
        set(p3_lo,'String',num2str(p3low)); 
    end

elseif strcmp(action,'changedistribution'),
   parameter1 = [2 10 5 20 1 10 2 0.5 0 2 5 5 5 0 5 2 5 0 2];
   p1high     = [4 10 10 20 2 10 5 1 1 3 10 10 10 2 5 3 10 0.5 3];
   p1low      = [0.5 2 1 5 0.5 5 2 0.25 -1 1 5 5 5 -2 1 1 5 0 1];
   p1lim      = [0 0 0 0 0 0 0 0 -Inf (1-eps) 1 1 1 -Inf 0 0 0 -Inf 0;...
              Inf Inf Inf Inf Inf Inf Inf 1 Inf Inf Inf Inf Inf Inf Inf Inf Inf (p2low - eps) Inf];

   parameter2 = [2 0.5 NaN NaN NaN 10 2 NaN 0.5 .5 5 1 1 1 NaN NaN NaN 1 2];
   p2high     = [4 1 NaN NaN NaN 10 5 NaN 1 0.99 10 5 5 2 NaN NaN NaN 2 3];
   p2low      = [0.5 0 NaN NaN NaN 5 2 NaN 0.1 0.1 5 -5 0.5 0.5 NaN NaN NaN 1 1];
   p2lim      = [0 (0 - eps) NaN NaN NaN 0 0 NaN 0 0 0 -Inf 0 0 NaN NaN NaN 0 0;...
              Inf (1+eps) NaN NaN NaN Inf Inf NaN Inf Inf Inf Inf Inf Inf NaN NaN NaN Inf Inf];

    set(p1limits,'Userdata',p1lim(:,popupvalue))
    set(p2limits,'Userdata',p2lim(:,popupvalue))
   if popupvalue == 1         %Beta distribution
        set(p1_text,'String','A');
        set(p2_text,'String','B');
        randtool('makevisible');

    elseif popupvalue == 2      %Binomial distribution
        set(p1_text,'String','Trials');
        set(p2_text,'String','Prob');
        randtool('makevisible');

    elseif popupvalue == 3      %Chisquare distribution
        set(p1_text,'String','df');

    elseif popupvalue == 4      %Discrete Uniform Distribution
        set(p1_text,'String','Number');
       
    elseif popupvalue == 5      %Exponential distribution
        set(p1_text,'String','Lambda');
       
    elseif popupvalue == 6      %F distribution
        set(p1_text,'String','df1');
        set(p2_text,'String','df2');
        randtool('makevisible');        

    elseif popupvalue == 7      %Gamma distribution
        set(p1_text,'String','A');
        set(p2_text,'String','B');
        randtool('makevisible');

    elseif popupvalue == 8      %Geometric distribution
        set(p1_text,'String','Prob');
       
    elseif popupvalue == 9      %Logormal distribution
        set(p1_text,'String','Mu');
        set(p2_text,'String','Sigma');
        randtool('makevisible');

    elseif popupvalue == 10      %Negative Binomial distribution
        set(p1_text,'String','R');
        set(p2_text,'String','Prob');
        randtool('makevisible');

    elseif popupvalue == 11      %Noncentral F distribution
        set(p1_text,'String','Nu1');
        set(p2_text,'String','Nu2');
        randtool('makevisible');

    elseif popupvalue == 12      %Noncentral T distribution
        set(p1_text,'String','Nu');
        set(p2_text,'String','Delta');
        randtool('makevisible');

    elseif popupvalue == 13      %Noncentral Chi-square distribution
        set(p1_text,'String','Nu');
        set(p2_text,'String','Delta');
        randtool('makevisible');

    elseif popupvalue == 14      %Normal distribution
        set(p1_text,'String','Mu');
        set(p2_text,'String','Sigma');
        randtool('makevisible');

    elseif popupvalue == 15      %Poisson distribution
        set(p1_text,'String','Lambda');
 
    elseif popupvalue == 16      %Rayleigh distribution
        set(p1_text,'String','B');
                 
    elseif popupvalue == 17      %T distribution
        set(p1_text,'String','df');
        
    elseif popupvalue == 18      %Uniform distribution
        set(p1_text,'String','Min');
        set(p2_text,'String','Max');
        randtool('makevisible');
 
    elseif popupvalue == 19      %Weibull distribution   
        set(p1_text,'String','A');
        set(p2_text,'String','B');
        randtool('makevisible');        
    end

   if popupvalue == 3 | popupvalue == 4 | popupvalue == 5 | popupvalue == 8 ...
      | popupvalue == 15 | popupvalue == 16 | popupvalue == 17,  
       set(p2_text,'Visible','off');
        set(p2_slider,'Visible','off');
        set(p2_field,'Visible','off');
        set(p2_lo,'Visible','off');
        set(p2_hi,'Visible','off');
   end

    set(p1_field,'String',num2str(parameter1(popupvalue)));
    set(p2_field,'String',num2str(parameter2(popupvalue)));
    set(p1_lo,'String',num2str(p1low(popupvalue)));
    set(p2_lo,'String',num2str(p2low(popupvalue)));
    set(p1_hi,'String',num2str(p1high(popupvalue)));
    set(p2_hi,'String',num2str(p2high(popupvalue)));
    set(p1_slider,'Min',p1low(popupvalue),'Max',p1high(popupvalue),'Value',parameter1(popupvalue));
    set(p2_slider,'Min',p2low(popupvalue),'Max',p2high(popupvalue),'Value',parameter2(popupvalue));

    randtool('setdata',2);

   maxx = max(max(get(histogram,'Xdata')));
   
   if popupvalue == 1
      set(gca,'XLim',[0 1]);
   end
   if popupvalue == 2
      set(gca,'XLim',[0 p1high(2)]);
   end
   if popupvalue == 3 | popupvalue == 5 | popupvalue == 6 | popupvalue == 7 | ...
      popupvalue == 8 | popupvalue == 9 | popupvalue == 10 | popupvalue == 11 | ...
      popupvalue == 13 | popupvalue == 15 | popupvalue == 19
       set(gca,'XLim',[0 1.2*maxx]);
   end
   if popupvalue == 4
       set(gca,'XLim',[0 p1high(4)]);
   end
   if popupvalue == 12
      set(gca,'XLim',[(p2low(12)-5) (p2high(12)+5)]);
   end
   if popupvalue == 14
      xl = [p1low(9) - 3 .* p2high(9) p1high(9) + 3 .* p2high(9)];
      set(gca,'XLim',xl);
   end
   if popupvalue == 17
      n10 = 10.*samples;
      p = [(1./n10) (1 - 1./n10)];
      set(gca,'XLim',[tinv(p,p1low(11))]);
   end
   if popupvalue == 18
         set(gca,'XLim',[p1low(12) p2high(12)]);
   end

    if popupvalue ~= 11 
       set(p3_text,'Visible','off');
        set(p3_slider,'Visible','off');
        set(p3_field,'Visible','off');
        set(p3_lo,'Visible','off');
        set(p3_hi,'Visible','off');
       set(p1_field,'Position',[.25 .08 .10 .05]);
        set(p1_hi,'Position',[.25 .08 .10 .05] + [.15 .06 0 0]);
        set(p1_lo,'Position',[.25 .08 .10 .05] + [.15 .06 0 0] + [0 -0.13 0 0]);
        set(p1_slider,'Position',[.25 .08 .10 .05] + [.11 -0.07 -0.07 0.13]);
        set(p1_text,'Position',[.25 .08 .10 .05] + [0 0.05 0 0]);

       set(p2_field,'Position',[.55 .08 .10 .05]);
        set(p2_hi,'Position',[.55 .08 .10 .05] + [.15 .06 0 0]);
        set(p2_lo,'Position',[.55 .08 .10 .05] + [.15 .06 0 0] + [0 -0.13 0 0]);
        set(p2_slider,'Position',[.55 .08 .10 .05] + [.11 -0.07 -0.07 0.13]);
        set(p2_text,'Position',[.55 .08 .10 .05] + [0 0.05 0 0]);

    else
       set(p1_field,'Position',[.17 .08 .08 .05]);
        set(p1_hi,'Position',[.17 .08 .08 .05] + [.13 .06 0 0]);
        set(p1_lo,'Position',[.17 .08 .08 .05] + [.13 -.07 0 0]);
        set(p1_slider,'Position',[.17 .08 .08 .05] + [.09 -0.07 -0.05 0.13]);
        set(p1_text,'Position',[.17 .08 .08 .05] + [0 0.05 0 0]);

       set(p2_field,'Position',[.40 .08 .08 .05]);
        set(p2_hi,'Position',[.40 .08 .08 .05] + [.13 .06 0 0]);
        set(p2_lo,'Position',[.40 .08 .08 .05] + [.13 -.07 0 0] );
        set(p2_slider,'Position',[.40 .08 .08 .05] + [.09 -0.07 -0.05 0.13]);
        set(p2_text,'Position',[.40 .08 .08 .05] + [0 0.05 0 0]);

       set(p3_field,'Position',[.63 .08 .08 .05]);
        set(p3_hi,'Position',[.63 .08 .08 .05] + [.13 .06 0 0]);
        set(p3_lo,'Position',[.63 .08 .08 .05] + [.13 -.07 0 0]);
        set(p3_slider,'Position',[.63 .08 .08 .05] + [.09 -0.07 -0.05 0.13]);
        set(p3_text,'Position',[.63 .08 .08 .05] + [0 0.05 0 0]);      

      set(p3_text,'Visible','on');
        set(p3_slider,'Visible','on');
        set(p3_field,'Visible','on');
        set(p3_lo,'Visible','on');
        set(p3_hi,'Visible','on');
   end
   
% End of changedistribution action.
    
elseif strcmp(action,'makevisible'),
   set(p2_text,'Visible','on');
   set(p2_slider,'Visible','on');
   set(p2_field,'Visible','on');
   set(p2_lo,'Visible','on');
   set(p2_hi,'Visible','on');

elseif strcmp(action,'output')
    if flag == 2
        matqueue('put',random_numbers);
      entrystr = 'r';
        promptstr = 'Enter a variable name for the random numbers or use the supplied default.';
        matqdlg('buffer2ws','PromptString',promptstr,'EntryString',entrystr);
   end

elseif strcmp(action,'stray_click'),
   set(gcf,'CurrentAxes',rand_axes);   
   
end     % End of long "case" statement.
