#!/usr/bin/perl # Perl script: MonteCarlo.pl # Authors: Arnab Saha-Mandal & Alexei Fedorov # Bioinformatics Lab, The University of Toledo Health Science Campus # Ohio, USA # January, 2013 # Contact arnab.sahamandal@rockets.utoledo.edu OR alexei.fedorov@utoledo.edu # license under GPL version 2 or later # If you use this code or incorporate it in another program please cite our paper "Saha-Mandal et.al", 2013. # This Perl script does the following: # In case the number of simulations are of the order of several millions or higher, # The program should run in background and output directed towards a file open (OUT, ">monte_carlo_output"); #initializing user inputs from keyboard print "Please enter the number of heads\n"; $v1 = ; print "Please enter the number of tails\n"; $v2 = ; print "For how many simulations do you want to run?\n"; $s = ; #putting the count to zero and looping the process over desired number of iterations $count = 0; for $x(1..$s){ $head=$tail=0; #variables for counting heads and tails initially set to zero #sublooping within a particular iteration for total number of coin tosses in an event for $n(1..($v1+$v2)){ #choosing a random number between 0 and 1 and counting outcomes within the event $r = int(rand(2)); if ($r == 1){ $head++; } else { $tail++; } } #closing the subloop #computing difference within the outcomes $d = abs($head - $tail); if ( $d >= abs($v2-$v1)){ $count++; #incrementing the count in case a difference greater than #expected is observed } } #closing the major loop after all the iterations have been completed #calculating the probability of fluctuation due to all the number of #iterations and thereby printing it onto the screen or using #"print OUT"(see beginning of code) if printing to a file $h = $count/$s; print "P-value = $h\n";