Mdmguyon said:
I may have been misleading in the first description of my problem. It is average time that I want to measure, not the average number of trials. It's a randomized slideshow in which no picture can repeat and each picture stays on the screen for differing durations. A simplified version would be a slideshow that has 3000 pictures, 2900 of which stay on the screen for 20 seconds and 100 of which stay on the screen for 5 minutes. There are 10 pictures which would stop the slideshow and the slideshow will stop after 2 hours and 40 minutes. What is the average duration of the slideshow? The only thing I can think of is to list each of 3000 possibilities in a randomized program and arrange for each picture that shows to not be able to repeat, so that I can run many trials and see what the average duration is, but that's too much trouble. Is there a way to do a randomized program and tell the number of each type of picture to be reduced by 1 each time one shows?
Is this what you want?
[CODE lang="perl" title="Perl program for simulation of 500 runs of slideshows"]
$numberOfSimulations=500;
foreach $simulation (1..$numberOfSimulations){
# make random set of 10 termination photos
foreach $termNumber (1..10){
$index = int(rand(3000));
$stopHere[$index] = 'y';
}
# Begin to run one simulation
while(1){
# Generate a random photo number
$randomPhotoIndex = int(rand(3000));
# Skip any photos already displayed
if( $alreadyDisplayed[$randomPhotoIndex] ){next}
# Record that this photo is displayed
$alreadyDisplayed[$randomPhotoIndex] = 'y';
# Add time for this photo display
if( $randomPhotoIndex < 2900 ){
$time += 20;
}else{
$time += 5*60;
}
# If this is a "stop photo", end this one simulation
if( $stopHere[$randomPhotoIndex] ){last}
# If maximum of 2 hours, 40 minutes is exceeded,
# set time at max and end this simulation
if( $time > (2*60+40)*60 ){
$time = (2*60+40)*60;
last;
}
}
# Save and print result of one simulation
$averageTime += $time/$numberOfSimulations;
print "Simulation $simulation: time=$time\n";
# Clear data from one simulation
$time=0;
undef @stopHere;
undef @alreadyDisplayed;
}
# Print final average from all simulations
print "averageTime=$averageTime\n";
$ans=<STDIN>;[/CODE]
Here is the last bit of data from a run:
[CODE title="Result of a run (last few simulations and total average)"]Simulation 474: time=25340
Simulation 475: time=5700
Simulation 476: time=9600
Simulation 477: time=9600
Simulation 478: time=9600
Simulation 479: time=4360
Simulation 480: time=1740
Simulation 481: time=4580
Simulation 482: time=9600
Simulation 483: time=2380
Simulation 484: time=6560
Simulation 485: time=8740
Simulation 486: time=9600
Simulation 487: time=3480
Simulation 488: time=9420
Simulation 489: time=6520
Simulation 490: time=340
Simulation 491: time=9600
Simulation 492: time=7880
Simulation 493: time=200
Simulation 494: time=320
Simulation 495: time=1660
Simulation 496: time=5760
Simulation 497: time=9600
Simulation 498: time=4740
Simulation 499: time=2120
Simulation 500: time=1340
averageTime=5892.83999999998
[/CODE]
Result of 5000 runs:
[CODE title="Result of 5000 simulations"]Simulation 4995: time=8460
Simulation 4996: time=720
Simulation 4997: time=2320
Simulation 4998: time=3980
Simulation 4999: time=5800
Simulation 5000: time=8940
averageTime=5731.8080000001[/CODE]
[CODE title="Result of 50,000 runs"]Simulation 49998: time=4760
Simulation 49999: time=2080
Simulation 50000: time=7060
averageTime=5749.37720000011[/CODE]