It's something like this :
I let you fill the end, it's simple.
setarray .@Hat$[0],5105,5106,5407; // You write all the id of hats here (50 pages on ratesmyserver gl )
set .@price,1000000;
set .@resrand,rand(xx); //xx is the number of id you added
if( Zeny > .@price ) {
set Zeny, Zeny - .@price;
getitem .@Hat$[.@resrand],1;
}
else {
message strcharinfo(0),"I'll need " + .@price + " zeny to have your random hat";
}