My output is wrong

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

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Roulette.cpp : This program will accumulate data for graphical analysis of the roulette strategy

using namespace 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;
}



And here is the output:


0
2
2
-1
-1
-1
-7
-7
-7
-7
-7
-7
-20
-20
-20
-20
-20


Does anyone know why I am getting this?
Last edited on
Please use [ code ] [ /code ] tags. I see srand() is being called from inside a loop, which is wrong. srand() should only be called once.
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.
Ok, I will change it, but that still doesnt tell me why my output is wrong. Does anyone have an opinion on that?
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.
Is this the real program? The above program will only output one line to the file:

1
2
3
for ( i=0; i < 100; i++) sum = sum + finalTotal[i]  // not wrapped in curly braces;
result = sum/100;
myfile << result << "\n";

EDIT: what Browni3141 said. The indentation fooled me.
Last edited on
@filipe: No, it appears this way because of his poor formatting. Note the closing brace for the for loop on line 81.
Okay, you see it.
Last edited on
Ok, I changed indentation for easier viewing. Sorry for the trouble.
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.
Also, you're not declaring i in each of the loops, but in a C style. Which makes things even more confusing and is certainly wrong.
Last edited on
result = sum/100;
Here your result is being truncated.
Ok, I changed the loop variables, and the output is different, but it still is not very random. Any advice?
Topic archived. No new replies allowed.