Hi. I wrote some code to analyze the outcome using a roulette strategy and put it into a file (which I will then use to make a graph). It goes through a loop using the same strategy with different starting funds, to see the various average profit after 100 times, and then outputs the result for each starting amount into a file. The numbers are outputting to the file just fine, but the numbers themselves dont make sense. It starts at 0 and just decreases by 1 every time
// Roulette.cpp : This program will accumulate data for graphical analysis of the roulette strategy
usingnamespace std;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <fstream>
int main(void)
{
int sum = 0;
int outcome[500];
int betCount = 1;
int startingBet;
int bet;
int startingMoney;
int endingMoney;
int money;
int profit = 0;
int interval;
int finalTotal[100];
int result;
// First a friendly introduction
cout << "What are the minimum starting funds? ";
cin >> startingMoney;
cout << "What are the maximum starting funds? ";
cin >> endingMoney;
cout << "what is the interval of increment?";
cin >> interval;
cout << "what is your starting bet? ";
cin >> startingBet;
bet = startingBet;
ofstream myfile ("money.txt", ios::out | ios::trunc);
if (myfile.is_open()) ;
else cout << "Unable to open file";
srand (time(NULL));
for(; startingMoney < endingMoney; startingMoney+=interval)
{
for (int i=0; i < 100; i++)
{
money = startingMoney;
/* initialize outcome: */
for (int j=0; j < 500; j++) outcome[j] = rand() % 380;
// now the betting algorithm
while (money >= bet)
{
betCount++;
if (outcome[betCount] > 179)
{
money = money + bet;
bet = startingBet;
profit = profit + money - startingMoney;
money = startingMoney;
}
else
{
money = money - bet;
bet = bet * 2;
}
if ((betCount > 100 && outcome[betCount] > 179) || bet > 500) break;
}
finalTotal[i] = money + profit - startingMoney;
profit = 0;
betCount = 0;
}
for (int k=0; k < 100; k++) sum = sum + finalTotal[k];
result = sum/100;
myfile << result << "\n";
}
myfile.close();
return 0;
}
I did it from inside the loop to reinitialize the seed. I dont want the same set of numbers every time, and there is no guarantee time(null) will give a different result on such small timescales
I did it from inside the loop to reinitialize the seed. I dont want the same set of numbers every time, and there is no guarantee time(null) will give a different result on such small timescales
No. That isn't how random number generators work. You only need to seed once with
srand(time(0)) or srand(time(NULL)). This will be sufficiently random.
It would be much easier if your code wasn't so difficult to read. Sorry, but it is. Your indentation is terrible. It is very hard to see that the closing brace on line 80 corresponds to the starting brace on line 46. I'm not trying to be hard on you, but you really should learn how to format code properly.
That's quite a bit better, thank you. Without looking into it too much I see that you've named all your loop variables 'i'. That will cause problems, like unpredictable results.