Jump to content
IGNORED

Programming with VBA in Excel


Guest Yegg

Recommended Posts

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? =[

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

 

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.

 

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

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.