function [p, histrange]=histp(sample, n, mini, maxi)
%    [p, histrange]=histp(sample, n, mini, maxi)
% INPUT
%    sample - sample to be summarized
%    n - the number of histogram bins
%    [mini, maxi] - the domain of the histogram
% OUTPUT
%    p - probabilities of bins
%    histrange - length of [mini, maxi]
% EXAMPLE 
% >> data = betarnd(2, 4, [1, 10000]);
% >> histp(data, 20, 0, 1);
% >> hold on
% >> plot( 0:0.005:1, betapdf(0:0.005:1, 2,4), 'r','linewidth',3)
%

if nargin == 2
    mini = min(sample);
    maxi = max(sample);
end
histrange = maxi-mini;
bsize=histrange/n;
fullrange = mini:bsize:maxi;
h=hist(sample, fullrange);  %generate histogram
p=h/(length(sample)*bsize); %normalize histogram
bar(fullrange, p);          %plot histogram