Hi all -
Brief history - long time back I wrote a VB program to simulate spinning a roulette wheel and was curious what the longest streak of any particular color would be over time (Black, Red, or Green). It's almost the same as a coin-flip type of program, but used Roulette because it was more interesting. That morphed into a Javascript/Web page that did it more graphically with tables and such. Then I moved it onto Powershell, and was getting a lot of speed (450,000 spins per minute, per PC that I ran it on). Not being satisfied and wanting to try another language, I just ported it all over to C++. I try to keep the logic all the same among languages to not introduce too many changes, but with C++ I'm getting about a million spins per SECOND on the same PCs. Amazing. I may port it over to Fortran for kicks but seriously, a million spins per second is impressive to me.
Anyway, program is pretty simple, starts up, picks a random number, checks the color associated with that spot on the roulette board I coded into a switch statement, if it's the same color as last time add to the high streak, if not set the streak back to one. Every one minute, write out the count of everything (and the PC Name) to a file, which gets aggregated via a DB stored procedure.
Highest streak with Powershell was 43, and I have a few PCs at 42.
C++ started on about 20 nodes, and they are stuck on 27 as a high streak, which seems odd to me. There's nothing that I'm seeing that would cause a stop at 27, so I'm wondering if this is something going on at a lower level that I'm not aware of (Compiler/random number/etc).
Code here. I haven't gone through to optimize it so to speak, I just wanted to get it running to do a speed test (to see if it's worth optimizing and learning more). It is/will be. .
Thank for any help on this silly experiment.
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <thread>
#include <chrono>
#include <cmath>
#include <ctime>
#pragma warning(disable : 4996);
using namespace std;
int main(int argc, char* argv[])
{
int streakCount = 0;
string colorPicked = "";
string lastColorPicked = "";
int highestStreak = 0;
unsigned long long spinCount = 0;
int wheelSpot = 0;
string PCName = getenv("COMPUTERNAME");
string fileStats = "C:\\XXX\\XXX\\" + PCName + ".txt";
time_t nowtime = time(NULL);
int minutes = 0;
int seconds = 0;
int ms = 0;
string fileLineText = "";
size_t firstCommaPos = 0;
size_t lastCommaPos = 0;
string str3 = "";
int fileLineTextLength = 0;
int fileWritten = 0;
ofstream outfile;
ifstream infile;
infile.open(fileStats);
if (infile) {
getline(infile, fileLineText);
fileLineTextLength = fileLineText.length();
//while (getline(infile, fileLineText)) {
// cout << fileLineText << endl;
//}
firstCommaPos = fileLineText.find_first_of(","); // position of first comma in line of text from file
PCName = fileLineText.substr(0, firstCommaPos); // load PCName variable
//cout << "PCName variable is " << PCName << endl;
str3 = fileLineText.substr(firstCommaPos); // get from firstCommaPos to the end
str3 = str3.substr(1);
// at this point we have stripped off the PC Name and first comma, so we have ####,#######
firstCommaPos = str3.find_first_of(",");
//str3 = str3.substr(0, firstCommaPos);
//spinCount = stoi(str3);
spinCount = stoull(str3);
//cout << "spinCount variable is " << spinCount << endl;
// at this point we have the spinCount variable loaded with data
//cout << "str3 variable " << str3 << endl;
firstCommaPos = str3.find_first_of(",");
str3 = str3.substr(firstCommaPos);
str3 = str3.substr(1);
highestStreak = stoi(str3);
//cout << "highestStreak variable is " << highestStreak << endl;
}
infile.close();
srand(time(0));
while (true) {
wheelSpot = rand() % 38 + 0;
switch (wheelSpot) {
case 0:
colorPicked = "Green";
break;
case 1:
colorPicked = "Red";
break;
case 2:
colorPicked = "Black";
break;
case 3:
colorPicked = "Red";
break;
case 4:
colorPicked = "Black";
break;
case 5:
colorPicked = "Red";
break;
case 6:
colorPicked = "Black";
break;
case 7:
colorPicked = "Red";
break;
case 8:
colorPicked = "Black";
break;
case 9:
colorPicked = "Red";
break;
case 10:
colorPicked = "Black";
break;
case 11:
colorPicked = "Black";
break;
case 12:
colorPicked = "Red";
break;
case 13:
colorPicked = "Black";
break;
case 14:
colorPicked = "Red";
break;
case 15:
colorPicked = "Black";
break;
case 16:
colorPicked = "Red";
break;
case 17:
colorPicked = "Black";
break;
case 18:
colorPicked = "Red";
break;
case 19:
colorPicked = "Red";
break;
case 20:
colorPicked = "Black";
break;
case 21:
colorPicked = "Red";
break;
case 22:
colorPicked = "Black";
break;
case 23:
colorPicked = "Red";
break;
case 24:
colorPicked = "Black";
break;
case 25:
colorPicked = "Red";
break;
case 26:
colorPicked = "Black";
break;
case 27:
colorPicked = "Red";
break;
case 28:
colorPicked = "Black";
break;
case 29:
colorPicked = "Black";
break;
case 30:
colorPicked = "Red";
break;
case 31:
colorPicked = "Black";
break;
case 32:
colorPicked = "Red";
break;
case 33:
colorPicked = "Black";
break;
case 34:
colorPicked = "Red";
break;
case 35:
colorPicked = "Black";
break;
case 36:
colorPicked = "Red";
break;
case 37:
colorPicked = "Green";
break;
}
if (colorPicked == lastColorPicked) {
streakCount++;
}
else {
streakCount = 1;
}
lastColorPicked = colorPicked;
if (streakCount > highestStreak) {
highestStreak = streakCount;
}
spinCount++;
nowtime = time(NULL);
//minutes = (nowtime / 60) % 60;
seconds = nowtime % 60;
if (seconds == 0) {
outfile.open(fileStats);
outfile << PCName << "," << spinCount << "," << highestStreak << endl;
outfile.close();
}
} // ends while loop
} // ends main
| |