Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

# Mathematical Inaccuracy?

3 replies to this topic

### #1 Glunn11

Glunn11

• Members
• 262 posts
• OFFLINE
•
• Gender:Male
• Location:Idaho
• Local time:07:50 PM

Posted 05 August 2008 - 08:38 PM

Hey BC! I've been making some simple C++ programs to test out some little online gambling games' house advantages with variable factors. These programs simply play the game 1 billion times and keep track of the wins and losses, then plug the wins and losses in place of favorable and unfavorable outcomes, 1 billion in place of the total outcomes, in the following equation:

(FAVORABLE x Win Potential / TOTAL OUTCOMES) + (UNFAVORABLE x Loss Potential / TOTAL OUTCOMES) = HA

To make sure the numbers I was getting were accurate, I mapped the possibilities of the simplest game: Pick-a-#. In this game, the bettor picks a number 1-6, and a dice is rolled. If that number is rolled, the bettor would win 3x his/her bet. In this instance, there is 1 favorable outcome, 5 unfavorable outcomes, a win potential of 2, and a loss potential of 1.

(1 x 2 / 6) + (5 x -1 / 6)
2/6 - 5/6
-3/6 = 50% HA

However, the program returns approximately 39.98% HA each time. The program is below - what am I doing wrong?
```#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;

int RandInt(int min, int max)
{
static bool isSeeded = false;

if (! isSeeded)
{
isSeeded = true;
}
return(rand() % (max - min) + min);
}

int main()
{
srand(time(NULL));
for (float potential = .5; potential <= 2.1; potential = potential + .1)
{
double wins = 0, losses = 0;
for (unsigned int wee = 0; wee < 1000000000; wee++)
{
int number = RandInt(1,6);
int choice = RandInt(1,6);
if (number == choice)
{
wins++;
}
else
{
losses++;
}
}
cout << "WIN: " << 1 + potential << endl;
cout << wins << " " << losses << "  " << wins + losses << endl;
cout << "Dealer's Advantage: " << (((wins * potential) / (wins + losses)) - (losses / (wins + losses))) * -1 << "\n\n\n";
}
return 0;
}```

Thanks!

### #2 jpshortstuff

jpshortstuff

WhatTheTech Teacher

• Members
• 660 posts
• OFFLINE
•
• Gender:Male
• Location:UK
• Local time:02:50 AM

Posted 08 August 2008 - 11:37 AM

Hi

Have a look at your RandInt function, and trace it through in your head with the numbers 1, 6 that you give it:

(rand() % (max - min) + min)
rand() % (6-1) + 1
rand() % 5 + 1
(number between 0 and 4) + 1
(number between 1 and 5)

You are losing the possibility of getting a six. This will cause problems for all your programs that call this function. To fix this, just add a one to your modulus:

(rand() % (max - min + 1) + min)

Hope that helps

Also, I don't understand why you have this at the top of your function:
```static bool isSeeded = false;

if (! isSeeded)
{
isSeeded = true;
}```

Cheers,

-jp
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here

### #3 Glunn11

Glunn11
• Topic Starter

• Members
• 262 posts
• OFFLINE
•
• Gender:Male
• Location:Idaho
• Local time:07:50 PM

Posted 08 August 2008 - 07:49 PM

Thank you for your reply! I guess that's what I deserve for taking another person's function for granted. The one I had been using previously stopped working - perhaps I forgot exactly how to write it. So, yeah: I'll clean it up a bit. Thanks again - this solves my problem

### #4 jpshortstuff

jpshortstuff

WhatTheTech Teacher

• Members
• 660 posts
• OFFLINE
•
• Gender:Male
• Location:UK
• Local time:02:50 AM

Posted 09 August 2008 - 11:06 PM

No problem, glad I could help v:0
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users