jeudi 23 juin 2016

VBA Integer vs Long for counting tries on check if random number is equal to picked number at a range of 1 to 100

Last week I had to take a test in VBA. I had to code a little "game". These were the rules:

  1. Pick a number between 1 and 100 (1 and 100 included)
  2. Call function Randomize
  3. Generate a random number between 1 and 100 (1 and 100 included) -> Int (100 * Rnd + 1)
  4. Check if random number = chosen number.
  5. 5.1 If true, add +1 to counter, print counter, game is finished
    5.2 If false, add+1 to counter, go back to Step 3.

I hope you get the aim of the "game".

I initialized the counter as an Integer (16 bit). My teacher told me that it is possible, that the counter might overflow. He recommends using a Long (32 bit) so that the chance to overflow is smaller.

I told him that it is nearly impossible to reach 32000 tries on the counter, because the chance that the picked number is equal to the generated number is 1:100.

He replied: But it's still possible.

My question:
Is it possible that the counter might overflow if the datatype is Integer? If yes, what's the chance? If no, how can I proof it?

Why is this question on stackoverflow and not on statistics?
It's simple. Because you guys know the Rnd-function and VBA behind the scenes, the guys on statistics don't.




Aucun commentaire:

Enregistrer un commentaire