Guest Yegg Posted February 3, 2010 Report Share Posted February 3, 2010 So I need to program a very simple stochastic simulation by tomorrow evening and I thought I could teach myself how to do it in time, but I was wrong. Can anyone please help me? =[ Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/ Share on other sites More sharing options...
Guest Masonic Boom Posted February 3, 2010 Report Share Posted February 3, 2010 Shouldn't you be using SAS for that kind of thing, rather than Excel? Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232350 Share on other sites More sharing options...
Guest Yegg Posted February 3, 2010 Report Share Posted February 3, 2010 No, this has to be in Excel. Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232352 Share on other sites More sharing options...
Guest Babar Posted February 3, 2010 Report Share Posted February 3, 2010 maybe you should post something specific. You could also try to post on a VBA dedicated forum although you might not get replies before tomorrow. Maybe irc is your best friend. Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232385 Share on other sites More sharing options...
kaini Posted February 3, 2010 Report Share Posted February 3, 2010 give us a bit more to work with ffs Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide kaini's signature Hide all signatures On 5/7/2013 at 11:06 PM, ambermonk said: I know IDM can be extreme On 6/3/2017 at 11:50 PM, ladalaika said: this sounds like an airplane landing on a minefield Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232399 Share on other sites More sharing options...
Guest Yegg Posted February 3, 2010 Report Share Posted February 3, 2010 Jeez, I figured anyone who was familiar with VBA could help. So here's the problem; Joe and 200 other job applicants are taking a 10 question true-false test to determine who gets the job. Joe has a studied and has a 90% chance of getting each question correct, whereas the other 200 applicants are just guessing and so have only 50% chance of answering each question correctly. If more than one person ties for the highest score, the winner is chosen at random. Develop a stochastic simulation to estimate Joe's chance of getting the job. What is your estimate? Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232400 Share on other sites More sharing options...
kaini Posted February 3, 2010 Report Share Posted February 3, 2010 (edited) hmmm well you'll need a function for the other applicants called within a loop iterated 199 times, and a function for joe. the first function will, for each applicant, write each applicant's correct number of answers to the first 199 spaces in an integer array (A) with the specified statistical skew. [loop - for each answer, pick a random number between 0 and 1. if it's less than 0.5, it's FALSE, ELSE TRUE. if true, increment X - end of loop] [write X to the correct index in A] the 200th space in A will be filled by the second function, which computes joe's answers with the specified statistical skew (pick a random number between 0 and 1. if it's less than 0.1, it's FALSE, ELSE TRUE). finally, you'll need to write a loop that iterates through A and determines if there are joint people sharing top spot (which it could flag through another 200-space boolean array, B) and if there are, it should select one at random. i could write it for you in C but not VBA cos it sux lol. Edited February 3, 2010 by kaini Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide kaini's signature Hide all signatures On 5/7/2013 at 11:06 PM, ambermonk said: I know IDM can be extreme On 6/3/2017 at 11:50 PM, ladalaika said: this sounds like an airplane landing on a minefield Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232409 Share on other sites More sharing options...
sneaksta303 Posted February 3, 2010 Report Share Posted February 3, 2010 :wtf: :wtf: :wtf: :wtf: :wtf: Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide sneaksta303's signature Hide all signatures The Dark Tower Cycle Pplz ep The Swarm H.P. Sneakstep's Educational Tours Vol. 1 Branch Acidian - Acid's Done Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232412 Share on other sites More sharing options...
Guest Yegg Posted February 3, 2010 Report Share Posted February 3, 2010 On 2/3/2010 at 11:37 PM, kaini said: hmmm well you'll need a function for the other applicants called within a loop iterated 199 times, and a function for joe. the first function will, for each applicant, write each applicant's correct number of answers to the first 199 spaces in an integer array (A) with the specified statistical skew. [loop - for each answer, pick a random number between 0 and 1. if it's less than 0.5, it's FALSE, ELSE TRUE. if true, increment X - end of loop] [write X to the correct index in A] the 200th space in A will be filled by the second function, which computes joe's answers with the specified statistical skew (pick a random number between 0 and 1. if it's less than 0.1, it's FALSE, ELSE TRUE). finally, you'll need to write a loop that iterates through A and determines if there are joint people sharing top spot (which it could flag through another 200-space boolean array, B) and if there are, it should select one at random. i could write it for you in C but not VBA cos it sux lol. Um, if anybody can translate this to code form, that would be nice. Also, the cells with the parameters should be in either column 2 or 3, I think. Kaini, do you have MSN? Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232426 Share on other sites More sharing options...
goffer Posted February 4, 2010 Report Share Posted February 4, 2010 Kaini do it for tits and settle for nothing less Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide goffer's signature Hide all signatures PHOTOS Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232435 Share on other sites More sharing options...
chassis Posted February 4, 2010 Report Share Posted February 4, 2010 Do it in C. Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide chassis's signature Hide all signatures Reveal hidden contents Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232442 Share on other sites More sharing options...
EdamAnchorman Posted February 4, 2010 Report Share Posted February 4, 2010 i took a class in undergrad where we wrote a ton of stuff in vba / excel. although this was 7 years ago and i remember almost nothing! Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide EdamAnchorman's signature Hide all signatures glowing in beige on the national stage Reveal hidden contents On 10/31/2007 at 9:17 PM, 'thejacketloose' said: On 10/31/2007 at 9:28 PM, 'Joyrex' said: Is that Eric Roberts? On 10/31/2007 at 9:31 PM, 'thejacketloose' said: Oh yeah. Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232455 Share on other sites More sharing options...
Guest Babar Posted February 4, 2010 Report Share Posted February 4, 2010 (edited) lol. hihihih i made it in java (as an exercise) here is what I found (but i think the numbers are pretty pointless heh ?) 100x 0.32 1000x 0.334 10000x 0.3279 also i'm not good at maths nor programming. And i'm on acid. whilst listening to lover's acid Edited February 4, 2010 by Babar Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232479 Share on other sites More sharing options...
Guest Yegg Posted February 4, 2010 Report Share Posted February 4, 2010 Ahh, okay. I can take it from here, thanks Kaini! Thankyouthankyouthankyou! Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232517 Share on other sites More sharing options...
kaini Posted February 4, 2010 Report Share Posted February 4, 2010 On 2/3/2010 at 11:54 PM, Yegg said: Um, if anybody can translate this to code form, that would be nice. Also, the cells with the parameters should be in either column 2 or 3, I think. Kaini, do you have MSN? i've given you the algorithm. i'll try and be clearer again and give you pseudocode. if you can't do it after that consider your choice of course. declare scoreresults = integer array [200] //hint: that's a column 1x200 in excel main { call function joe call function otherapplicants call function sortscores call function displayresults } function joe { declare float joe_result declare integer joe_count = 0 loop 10 times { let joe_result = a random number between 0 and 1 if joe_result is greater than 0.1, increment joe_count } write joe_count to scoreresults [0] } function otherapplicants { declare integer applicant = 1 //0 is taken by joe declare float applicant_result declare integer applicant_count = 0 loop 199 times { loop 10 times { let applicant_result = a random number between 0 and 1 if applicant_result is greater than 0.5, increment applicant_count } write applicant_count to scoreresults [applicant] increment applicant } } pt 2 after i catch my breath Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide kaini's signature Hide all signatures On 5/7/2013 at 11:06 PM, ambermonk said: I know IDM can be extreme On 6/3/2017 at 11:50 PM, ladalaika said: this sounds like an airplane landing on a minefield Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232520 Share on other sites More sharing options...
kaini Posted February 4, 2010 Report Share Posted February 4, 2010 (edited) function sortscores { declare integer joepos declare integer winner call function arraysort call function findwinners function arraysort { // here you get to write a bubblesort algorithm, computing 101. // the only variation from an absolutely standard one is if the value // in scoreresults [0] is moved during the sort, you must record the new position to joepos. } function findwinners { declare integer numwins declare integer x = 0 loop 199 times { if scoreresults [x] = scoreresults [x+1] then increment numwins increment x } // you now know how many people share top place, it is scoreresults [0..numwins-1], or numwins basically with joe's place in the rank recorded in joepos } you have very little to do now. i'm not doing all your fucking homework. Edited February 4, 2010 by kaini Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide kaini's signature Hide all signatures On 5/7/2013 at 11:06 PM, ambermonk said: I know IDM can be extreme On 6/3/2017 at 11:50 PM, ladalaika said: this sounds like an airplane landing on a minefield Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232554 Share on other sites More sharing options...
Guest Yegg Posted February 4, 2010 Report Share Posted February 4, 2010 Hey, I didn't ask you to! I just needed a nudge in the right direction. Thanks again, kainiii!!! Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232559 Share on other sites More sharing options...
kaini Posted February 4, 2010 Report Share Posted February 4, 2010 there's a flaw in my code. the winner-counting algorithm is incorrect but i'm too wasted to fix it what you need to isolate is how many people at the top of the table share a score - as soon as the score drops, you stop counting. you also need to retain what position in the table (joepos) joe is at. you don't even need to retain his score, you can get that by looking at scoreresults [joepos] Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide kaini's signature Hide all signatures On 5/7/2013 at 11:06 PM, ambermonk said: I know IDM can be extreme On 6/3/2017 at 11:50 PM, ladalaika said: this sounds like an airplane landing on a minefield Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232572 Share on other sites More sharing options...
EdamAnchorman Posted February 4, 2010 Report Share Posted February 4, 2010 (edited) i could do it for you in labview, lol. its the only programming language i know Edited February 4, 2010 by Charles Nelson Reilly Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide EdamAnchorman's signature Hide all signatures glowing in beige on the national stage Reveal hidden contents On 10/31/2007 at 9:17 PM, 'thejacketloose' said: On 10/31/2007 at 9:28 PM, 'Joyrex' said: Is that Eric Roberts? On 10/31/2007 at 9:31 PM, 'thejacketloose' said: Oh yeah. Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232574 Share on other sites More sharing options...
kaini Posted February 4, 2010 Report Share Posted February 4, 2010 you could always ask over at techspeakblog, those guys know their fuckin stuff. Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide kaini's signature Hide all signatures On 5/7/2013 at 11:06 PM, ambermonk said: I know IDM can be extreme On 6/3/2017 at 11:50 PM, ladalaika said: this sounds like an airplane landing on a minefield Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232578 Share on other sites More sharing options...
Guest Babar Posted February 4, 2010 Report Share Posted February 4, 2010 (edited) i'm posting my source even though i'm not sure it's correct. 1. java uses classes. You invoke a class with : Class _className = new Class(_parameters); 2. Class contain functions. You call them this way : _className.function(_parameters); 3. I used the nextInt method function from the Random class. nextInt(x) gives you a number among [0;x[ and i think it's all you need to know in order to understand my code. What I did is finding the maximum among the others then counting how many of them had reached the highest score. Quote import java.util.Random; public class Yegg{ public static void main (String args []){ Test _test = new Test(); System.out.print("100x\t"); int _joeWin = 0; for (int i=0 ; i<100 ; i++){ if(_test.run()==true){ _joeWin++; } } double _joeProb = (double)_joeWin/100; System.out.println(_joeProb); System.out.print("1000x\t"); _joeWin = 0; for (int i=0 ; i<1000 ; i++){ if(_test.run()==true){ _joeWin++; } } _joeProb = (double)_joeWin/1000; System.out.println(_joeProb); System.out.print("10000x\t"); _joeWin = 0; for (int i=0 ; i<10000 ; i++){ if(_test.run()==true){ _joeWin++; } } _joeProb = (double)_joeWin/10000; System.out.println(_joeProb); } } class Test{ public boolean run(){ //Joe Joe _joe = new Joe(); int _joeScore = _joe.test(); int[] _scores = new int[200]; //Others for (int i=0 ; i<200 ; i++){ Other _other = new Other(); int _otherScore = _other.test(); _scores = _otherScore; } //getting the maximum score among others int _max = getMax(_scores); if(_joeScore>_max){ return true; } else if(_joeScore<_max){ return false; } else {//randomly picks a winner //counts how many 'others' have reached the highest score int _x = howMany(_max, _scores); //don't forget to add joe to the pool _x++; Random _r = new Random(); if(_r.nextInt(_x)!=0){ return false; } else{ return true; } } } private int getMax(int[] _scores){ int _max = 0; for (int i=0 ; i<_scores.length ; i++){ if (_scores>_max){ _max = _scores; } } return _max; } private int howMany(int _max, int[] _scores){ int _x = 0; for (int i=0 ; i<_scores.length ; i++){ if (_scores==_max){ _x++; } } return _x; } } class Joe{ public int test(){ int _score = 0; Random _r = new Random(); for(int i=0 ; i<10 ; i++){ //_r.nextInt(10) if(_r.nextInt(10)!=0){ _score++; } } return _score; } } class Other{ public int test(){ int _score = 0; Random _r = new Random(); for(int i=0; i<10 ; i++){ if(_r.nextInt(10)%2==0){//half the numbers are multiples of 2 _score++; } } return _score; } } oh and there was a glitch in my previous code, so my results are 100x 0.48 1000x 0.45 10000x 0.4468 i'm posting my source even though i'm not sure it's correct. 1. java uses classes. You invoke a class with : Class _className = new Class(_parameters); 2. Class contain functions. You call them this way : _className.function(_parameters); 3. I used the nextInt method function from the Random class. nextInt(x) gives you a number among [0;x[ and i think it's all you need to know in order to understand my code. What I did is finding the maximum among the others then counting how many of them had reached the highest score. Quote import java.util.Random; public class Yegg{ public static void main (String args []){ Test _test = new Test(); System.out.print("100x\t"); int _joeWin = 0; for (int i=0 ; i<100 ; i++){ if(_test.run()==true){ _joeWin++; } } double _joeProb = (double)_joeWin/100; System.out.println(_joeProb); System.out.print("1000x\t"); _joeWin = 0; for (int i=0 ; i<1000 ; i++){ if(_test.run()==true){ _joeWin++; } } _joeProb = (double)_joeWin/1000; System.out.println(_joeProb); System.out.print("10000x\t"); _joeWin = 0; for (int i=0 ; i<10000 ; i++){ if(_test.run()==true){ _joeWin++; } } _joeProb = (double)_joeWin/10000; System.out.println(_joeProb); } } class Test{ public boolean run(){ //Joe Joe _joe = new Joe(); int _joeScore = _joe.test(); int[] _scores = new int[200]; //Others for (int i=0 ; i<200 ; i++){ Other _other = new Other(); int _otherScore = _other.test(); _scores = _otherScore; } //getting the maximum score among others int _max = getMax(_scores); if(_joeScore>_max){ return true; } else if(_joeScore<_max){ return false; } else {//randomly picks a winner //counts how many 'others' have reached the highest score int _x = howMany(_max, _scores); //don't forget to add joe to the pool _x++; Random _r = new Random(); if(_r.nextInt(_x)!=0){ return false; } else{ return true; } } } private int getMax(int[] _scores){ int _max = 0; for (int i=0 ; i<_scores.length ; i++){ if (_scores>_max){ _max = _scores; } } return _max; } private int howMany(int _max, int[] _scores){ int _x = 0; for (int i=0 ; i<_scores.length ; i++){ if (_scores==_max){ _x++; } } return _x; } } class Joe{ public int test(){ int _score = 0; Random _r = new Random(); for(int i=0 ; i<10 ; i++){ //_r.nextInt(10) if(_r.nextInt(10)!=0){ _score++; } } return _score; } } class Other{ public int test(){ int _score = 0; Random _r = new Random(); for(int i=0; i<10 ; i++){ if(_r.nextInt(10)%2==0){//half the numbers are multiples of 2 _score++; } } return _score; } } oh and there was a glitch in my previous code, so my results are 100x 0.48 1000x 0.45 10000x 0.4468 double post + no tabs = forget it Edited February 4, 2010 by Babar Quote Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232660 Share on other sites More sharing options...
kaini Posted February 4, 2010 Report Share Posted February 4, 2010 actually it's an interesting problem. it is an experiment in P = NP if properly parametarised. time-to-solve is somewhere between log and exp. Thanks Haha Confused Sad Facepalm Burger Farnsworth Big Brain Like × Quote Hide kaini's signature Hide all signatures On 5/7/2013 at 11:06 PM, ambermonk said: I know IDM can be extreme On 6/3/2017 at 11:50 PM, ladalaika said: this sounds like an airplane landing on a minefield Link to comment https://forum.watmm.com/topic/53126-programming-with-vba-in-excel/#findComment-1232663 Share on other sites More sharing options...
Recommended Posts