You should put \makeatletter and \makeatother around the definition of \declarenumlist and move \pgfmathrandomitem\z{total} outside the argument to \includegraphics like in
\pgfmathrandomitem\z{total}\includegraphics{\z.pdf}
However I would recommend a different definition of \declarenumlist that doesn't depend on the actual way a list for random choice is internally built by TikZ/PGF.
In the code below the list of numbers from the lowest bound to the highest bound is generated and fed as argument to \pgfmathdeclarerandomlist. The loop is very similar, but, as said, the macro will continue to work even if TikZ/PGF changes the internals of these lists.
Next I define an interface for \includegraphics: the \randomincludegraphics command accepts options to pass to \includegraphics and its mandatory argument is the list name.
Since I don't have the files to play with, I just hid the real macro to call and used a mock text for showing the effect.
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\begin{document}
\makeatletter
\def\declarenumlist#1#2#3{%
\begingroup
\count@=#2\relax
\def\x{}%
\loop
\edef\x{\x{\the\count@}}%
\ifnum\count@<#3\relax
\advance\count@@ne
\repeat
\edef\x{\endgroup
\noexpand\pgfmathdeclarerandomlist{#1}{\x}%
}\x
}
\makeatother
\newcommand{\randomincludegraphics}[2][]{%
\begingroup
\pgfmathrandomitem\z{#2}%
% Uncomment the following line for the production version
% \includegraphics[#1]{\z}%
% and remove the following line
I want to include \texttt{\z.pdf} with options ``\texttt{#1}''%
\endgroup
}
%I want 58 numbers because I have 58 images
\declarenumlist{total}{1}{58}
\randomincludegraphics{total}
\randomincludegraphics{total}
\randomincludegraphics[height=3cm,width=1cm]{total}
\end{document}

Here's a version that avoids repetitions; if it's called more times than the available items, random repetitions are used (with a warning). You have to redeclare the list, if you want to use it another time.
\documentclass[a4paper,12pt]{article}
\usepackage{tikz}
\begin{document}
\makeatletter
\def\declarenumlist#1#2#3{%
@ifundefined{c@#1@listcount}{\newcounter{#1@listcount}}{}%
\setcounter{#1@listcount}{1}%
\begingroup
\count@=#2\relax
\def\x{}%
\loop
\edef\x{\x{\the\count@}}%
\global\expandafter\let\csname#1@\number\count@ @used\endcsname\relax
\ifnum\count@<#3\relax
\advance\count@@ne
\stepcounter{#1@listcount}%
\repeat
\edef\x{\endgroup
\noexpand\pgfmathdeclarerandomlist{#1}{\x}%
}\x
\expandafter\mathchardef\csname #1@number\endcsname\value{#1@listcount}%
\setcounter{#1@listcount}{0}%
}
\newcommand{\pgfmathuniquerandomitem}[2]{%
\pgfmathrandomitem#1{#2}%
\ifnum\value{#2@listcount}=@nameuse{#2@number}%
@latex@warning{List #2 exhausted}%
\else
@ifundefined{#2@#1@used}%
{\stepcounter{#2@listcount}\global@namedef{#2@#1@used}{used}}%
{\pgfmathuniquerandomitem#1{#2}}%
\fi
}
\makeatother
\newcommand{\randomincludegraphics}[2][]{%
\begingroup
\pgfmathuniquerandomitem\z{#2}%
% Uncomment the following line for the production version
% \includegraphics[#1]{\z}
% and remove the following line
I want to include \texttt{\z.pdf} with options ``\texttt{#1}''
\endgroup
}
%I want 58 numbers because I have 58 images
\declarenumlist{total}{1}{58}
\randomincludegraphics{total}
\randomincludegraphics{total}
\randomincludegraphics[height=3cm,width=1cm]{total}
\foreach\i in {1,...,58} { \randomincludegraphics{total}\endgraf }
\declarenumlist{total}{1}{58} % redeclare the list
\foreach\i in {1,...,58} { \randomincludegraphics{total}\endgraf }
\end{document}
Update 2022
There are much better ways now to do the same task.
\documentclass[a4paper]{article}
\ExplSyntaxOn
% just for using all items
\NewDocumentCommand{\myforeach}{m +m}
{
\int_step_inline:nn { #1 } { #2 }
}
%%%%
\NewDocumentCommand{\declarerandomlist}{mO{1}m}
{% #1 = list name
% #2 = start point (default 1)
% #3 = end point
\egreg_randomlist_declare:nnn { #1 } { #2 } { #3 }
}
\NewDocumentCommand{\usefromrandomlist}{mm}
{% #1 = list name
% #2 = template
\egreg_randomlist_use:nn { #1 } { #2 }
}
\cs_new_protected:Nn \egreg_randomlist_declare:nnn
{
\seq_clear_new:c { l_egreg_randomlist_#1_seq }
\bool_do_until:nn { \int_compare_p:n { \seq_count:c { l_egreg_randomlist_#1_seq } = #3-#2+1 } }
{
\seq_put_left:cx { l_egreg_randomlist_#1_seq } { \int_rand:nn { #2 } { #3 } }
\seq_remove_duplicates:c { l_egreg_randomlist_#1_seq }
}
}
\cs_new_protected:Nn \egreg_randomlist_use:nn
{
\cs_set:Nn __egreg_randomlist:n { #2 }
\seq_if_empty:cTF { l_egreg_randomlist_#1_seq }
{
\msg_warning:nnn { randomlist } { exhausted } { #1 }
}
{
\seq_pop_left:cN { l_egreg_randomlist_#1_seq } \l__egreg_randomlist_item_tl
__egreg_randomlist:V \l__egreg_randomlist_item_tl
}
}
\tl_new:N \l__egreg_randomlist_item_tl
\cs_new_protected:Nn __egreg_randomlist:n { } % initialize
\cs_generate_variant:Nn __egreg_randomlist:n { V }
\msg_new:nnn { randomlist } { exhausted } {List ~ #1 ~ exhausted}
\ExplSyntaxOff
\newcommand{\randomincludegraphics}[2][]{%
% Uncomment the following line for the production version
% \usefromrandomlist{#2}{\includegraphics[#1]{##1}
% and remove the following line
\usefromrandomlist{#2}{I want to include \texttt{##1.pdf} with options ``\texttt{#1}''}
}
\begin{document}
\declarerandomlist{total}{5}
\randomincludegraphics{total}
\randomincludegraphics{total}
\randomincludegraphics[height=3cm,width=1cm]{total}
\myforeach{5}{\randomincludegraphics{total}\par}
\end{document}
The main difference, apart from the names, is that
\usefromrandomlist
takes as arguments the list name and a template where #1 stands for the current item in the random list, so one can do
\usefromrandomlist{total}{\includegraphics{#1.pdf}}
or abstract the command (like in the code above), remembering that #1 has to become ##1.

\number, which is a very important TeX primitive. – egreg Nov 14 '13 at 17:01\numberhere because this was the first variable name that came to my mind... I will update my question with a less contentious variable name. – darpich Nov 14 '13 at 17:03