I'm struggling to move away from a procedural coding background and learn good Mathematica functionality. Most commonly, my issue is with generating lists where a little computation is required to determine the entry in each list. My understanding is that the Table function should be used in creating lists, but I struggle to make it work in these kind of situations. To give in example, suppose I am drawing random numbers from [0,10] and I wanted to estimate the expected value of how many draws are required to get a number > 5. One way would be to do a bunch of trials and for each trial see how many numbers I had to draw to get one > 5. Given the number of trials, numRums, I'd like to store this in a list. I can do this with a for loop:
HowManyTimesList = {};
numRuns = 10;
For[i = 1, i <= numRuns, i++,
iter = 1;
While[RandomReal[{0, 10}] < 5, iter++]
AppendTo[HowManyTimesList, iter];
]
N[Mean[HowManyTimesList]]
But this seems ugly and un-Mathematica-esque. My struggle with using a Table is that I need to loop through doing draws until I have a number > 5, but I then need to return the number of times. I can't fit that into the Table function because it seems to only take single expression as the first argument.
I imagine there might be clever ways to organize this particular example so that it returns the desired expression in a single line, and then can be squeezed into a Table. But it seems like there should be a nice clean way to make lists where you don't always have to squeeze things into a single returnable line. So how do you approach such situations in a proper Mathematica way?