What is this:
This is a simple function, part of a basic Monte Carlo simulation. It takes sample size and probability as parameters. It returns the simulation result (positive answers) plus the input parameters in a tuple.
What I’m asking:
I’m trying to avoid using temporary variables, I have two questions.
- Do I really save memory by avoiding storing interim results?
- How could I improve readability without adding variables?
def simulate_survey(sample_size, percent_subscribes): return ( sample_size, percent_subscribes, round( ( sum([ r.random() < percent_subscribes for _ in range(sample_size) ]) / sample_size ), 2 ) )
Thank you for your feedback.