function [M,I] = softmax1_chev(X,temperature,chev) 

% SOFTMAX1_CHEV --  SOFTMAX1 with externally supplied "chance event"
%
% M = softmax1_chev(X,temperature,chev)
% [M,I] = softmax1_chev(X,temperature,chev)
%
% Exactly identical to SOFTMAX1 but the "chance event" is supplied from
% outside instead of generated internally (via RAND). This allows to
% generate chance events in batches, to re-use them for different model
% parameters, etc.
%
% CHEV is a scalar random deviate. SOFTMAX1 uses chev = rand ; 
% X, TEMPERATURE, M, and I are exactly as in SOFTMAX1. Same semantics.
%
% See also SOFTMAX, ANYRND1.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2007/08/14 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2007, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

if (temperature < eps)
  [M,I] = max(X) ;
else
  p = exp(X./temperature) ;
  cum_p = cumsum(p./sum(p)) ;
  hits = (cum_p <= chev) ;
  I = sum(hits) + 1 ;
  M = X(I) ;
end

%-- Return M and I
%%%%% End of SOFTMAX1_CHEV.M
