(*3x+1 function *)
f[x_] := If[Mod[x,2] == 0, x/2,3x+1];
g[x_] := Module[{},
(* input is an even number *)
temp = 3x+1;
While[Mod[temp,2]  == 0, temp = temp/2];
Return[temp];
];

iteratef[start_,print_] := Module[{},
count = 0;
current = start;
Print["We are starting at ", current, "."];
While[current > 1, 
{
current = f[current];
count  = count + 1;
If[print == 1,Print[current]];
}]; (* end of while loop *)
];

iteratefrange[numdo_,startpower_,function_] := Module[{},
results = {};
h[y_] :=  If[function == 1,f[y],g[y]];
For[n =1, n ≤ numdo, n++,
{
count = 0;
current = RandomInteger[10^startpower];
While[current > 1, 
{
current =h[current];
count  = count + 1;
}]; (* end of while loop *)
results = AppendTo[results,count];
}]; (* end of n loop *)
Print["Doing ", numdo, "  runs starting at 10^", startpower];
Print["Average is ",1.0Mean[results]];
Print["StDev is ", 1.0StandardDeviation[results]];
Print[Histogram[results, Automatic, "Probability"]];
];

Timing[iteratefrange[100000,1000,2]]