function r = eml_randn(cmd,varargin)
%MATLAB Code Generation Private Function

%   This function exists to support RANDN legacy mode. When not in legacy
%   mode, it is little more than eml_rand('generate_normal',varargin{:}).
%
%   USAGE:
%   method = eml_randn('method') -- DEFAULT, V4 or V5NORMAL
%   eml_randn('method',method) -- set method
%   state = eml_randn('state') -- returns legacy state.
%   eml_randn('state',state) -- set legacy state.
%   r = eml_rand('generate',<size arguments>,<class>)

%   Copyright 2005-2014 The MathWorks, Inc.
%#codegen

coder.internal.prefer_const(cmd);
coder.internal.prefer_const(varargin);
DEFAULT = eml_rand_str2id('default');
persistent method state
if isempty(method)
    method = DEFAULT;
    state = eml_rand_shr3cong('default_state');
end
switch coder.const(coder.internal.toLower(cmd))
    case 'method'
        if nargin == 1
            r = method;
        else
            method = varargin{1};
        end
    case 'state'
        if nargin == 1
            r = state;
        else
            state = varargin{1};
        end
    case 'generate'
        if method == DEFAULT
            r = eml_rand('generate_normal',varargin{:});
        else
            % Legacy mode
            % Preallocate return value.
            if nargin > 1 && ischar(varargin{end})
                eml_invariant(coder.internal.isFloatClass(varargin{end}), ...
                    'MATLAB:randn:invalidOutputType', ...
                    'IfNotConst','Fail');
            end
            r = coder.nullcopy(zeros(varargin{:}));
            % Generate random numbers.
            if method == eml_rand_str2id('mcg16807')
                for k = 1:numel(r)
                    [state(1),r(k)] = eml_rand_mcg16807('generate_normal',state(1));
                end
            else % if method == V5
                for k = 1:numel(r)
                    [state,r(k)] = eml_rand_shr3cong('generate_normal',state);
                end
            end
        end
    otherwise
end

