Instead of blowing against the wind, I changed my super lotto program to work on IRIX, AIX & Windows using ostringstream to convert the INTs to Strings.
First off the board was the random seeding issue. which I had implemented wrong in the first place but it did leave me wondering..
Basically the program:
Generates as many random lottery draws of 6 numbers as inputted by the user.
Adds a count of each number drawn.
Calculates the highest drawn 6 & the lowest drawn 6.
At first this was done through a loop which went through the list sequentially, but that proved to be biased towards the end numbers in the loop, so say number 1,4,5,6,7,8,10 had been drawn 100 times, number 1 would always be pushed off the list. To counter this, each number taken from the list index was also a random number, so 10 could be checked first and maybe 1 last.
Each time a random number was obtained, a new seed was created and this was my mistake.
The IRIX, AIX compile would always get stuck finding the last 2 entries in the list index whilst sorting, yet the windows always worked fine, this had me scratching my head with possible timing issues. I had a loop to delay the time for the new seed by 1 second, and yet it was never able to sort the last 2 numbers.
I changed this to produce a single seed at the beginning, all was well and I was able to remove any sleeps, pauses or loops. To speed things up further, I may remove each entry checked from the list and reduce the random number for the list index sorting.
The AIX can produce a 10,000 lotto sample in about 4 mins, as does the windows machine which is an i7 - Quite impressive I thought. The SGI (300MHz Octane2 - 1.5 GB of memory) is still running and has taken half an hour so far.
Below is the code taken from the windows machine because I'm on it right now. It's exactly the same apart from a few headers in and out.
I may do some time trials on my machines and maybe win the lotto in the process.
Code:
Select all
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <list>
//#include <unistd.h>
#include <ctime>
#include <cstdlib>
#include <algorithm>
int generateRandomNumber()
{
int iReturn = 0;
iReturn = rand() % 59 + 1;
return iReturn;
}
int main()
{
time_t timer;
struct tm y2k = { 0 };
int seconds;
y2k.tm_hour = 0; y2k.tm_min = 0; y2k.tm_sec = 0;
y2k.tm_year = 100; y2k.tm_mon = 0; y2k.tm_mday = 1;
time(&timer); /* get current time; same as: timer = time(NULL) */
seconds = difftime(timer, mktime(&y2k));
seconds = seconds % 10000000;
srand((int)seconds);
using std::cin;
using std::cout;
using std::endl;
using std::getchar;
using std::list;
using std::sort;
using std::string;
using std::stringstream;
string sExit = "n";
int iNumberCount1[60] = {};
int iNumberCount2[60] = {};
int iRandom1=0;
int iRandom2=0;
int iRandom3=0;
int iRandom4=0;
int iRandom5=0;
int iRandom6=0;
int iMainLoopMax = 0;
time_t now = time(0);
char* dt = ctime(&now);
cout << "\tThe local date and time is: " << dt << endl;
list<int> lList;
int iListCount = 1;
cout << endl << endl << "ENTER SAMPLE AMMOUNT (MINIUM 61):";
while (!(cin >> iMainLoopMax))
{
cout << "INCORRECT VALUE";
cin.clear();
}
for (int iMainLoop = 0; iMainLoop < iMainLoopMax; iMainLoop++)
{
int iSkip1 = 0;
int iSkip2 = 0;
int iSkip3 = 0;
int iSkip4 = 0;
int iSkip5 = 0;
int iSkip6 = 0;
cout << "Remaing iterations: " << iMainLoopMax - iMainLoop << endl;
iListCount = 0;
lList.clear();
while (iListCount < 7)
{
do
{
iRandom1 = generateRandomNumber();
if (iRandom1 != iRandom2 &&
iRandom1 != iRandom3 &&
iRandom1 != iRandom4 &&
iRandom1 != iRandom5 &&
iRandom1 != iRandom6)
{
cout << "\t\tDraw Number: " << iRandom1 << endl;
lList.push_front(iRandom1);
lList.sort();
lList.unique();
iListCount = lList.size();
iSkip1 = 1;
cout << "List Count: " << iListCount << endl <<
" iRandom1 " << endl;
}
} while (iSkip1 == 0);
if (iListCount == 6)
break;
do
{
iRandom2 = generateRandomNumber();
if (iRandom2 != iRandom1 &&
iRandom2 != iRandom3 &&
iRandom2 != iRandom4 &&
iRandom2 != iRandom5 &&
iRandom2 != iRandom6)
{
cout << "\t\tDraw Number: " << iRandom2 << endl;
lList.push_front(iRandom2);
lList.sort();
lList.unique();
iListCount = lList.size();
iSkip2 = 1;
cout << "List Count: " << iListCount << endl <<
" iRandom2 " << endl;
}
} while (iSkip2 == 0);
if (iListCount == 6)
break;
do
{
iRandom3 = generateRandomNumber();
if (iRandom3 != iRandom1 &&
iRandom3 != iRandom2 &&
iRandom3 != iRandom4 &&
iRandom3 != iRandom5 &&
iRandom3 != iRandom6)
{
cout << "\t\tDraw Number: " << iRandom3 << endl;
lList.push_front(iRandom3);
lList.sort();
lList.unique();
iListCount = lList.size();
iSkip3 = 1;
cout << "List Count: " << iListCount << endl <<
" iRandom3 " << endl;
}
} while (iSkip3 == 0);
if (iListCount == 6)
break;
do
{
iRandom4 = generateRandomNumber();
if (iRandom4 != iRandom1 &&
iRandom4 != iRandom2 &&
iRandom4 != iRandom3 &&
iRandom4 != iRandom5 &&
iRandom4 != iRandom6)
{
cout << "\t\tDraw Number: " << iRandom4 << endl;
lList.push_front(iRandom4);
lList.sort();
lList.unique();
iListCount = lList.size(); iSkip4 = 1;
iSkip4 = 1;
cout << "List Count: " << iListCount << endl <<
" iRandom4 " << endl;
}
} while (iSkip4 == 0);
if (iListCount == 6)
break;
do
{
iRandom5 = generateRandomNumber();
if (iRandom5 != iRandom1 &&
iRandom5 != iRandom2 &&
iRandom5 != iRandom3 &&
iRandom5 != iRandom4 &&
iRandom5 != iRandom6)
{
cout << "\t\tDraw Number: " << iRandom5 << endl;
lList.push_front(iRandom5);
lList.sort();
lList.unique();
iListCount = lList.size();
iSkip5 = 1;
cout << "List Count: " << iListCount << endl <<
" iRandom5 " << endl;
}
} while (iSkip5 == 0);
if (iListCount == 6)
break;
do
{
iRandom6 = generateRandomNumber();
if (iRandom6 != iRandom1 &&
iRandom6 != iRandom2 &&
iRandom6 != iRandom3 &&
iRandom6 != iRandom4 &&
iRandom6 != iRandom5)
{
iRandom6 = generateRandomNumber();
cout << "\t\tDraw Number: " << iRandom6 << endl;
lList.push_front(iRandom6);
lList.sort();
lList.unique();
iListCount = lList.size();
iSkip6 = 1;
cout << "List Count: " << iListCount << endl <<
" iRandom6 " << endl;
}
} while (iSkip6 == 0);
if (iListCount == 6)
break;
cout << "Draw List Count: " << iListCount << endl;
}
cout << "-----------------------------------------" << endl;
lList.unique();
iListCount = lList.size();
for (list<int>::iterator list_iter = lList.begin();
list_iter != lList.end(); list_iter++)
{
cout << "\t\tLotto Number: " << *list_iter << endl;
iNumberCount1[*list_iter]++;
iNumberCount2[*list_iter]++;
}
//I can't recall why this is here FFS!!
for (int iWait = 0; iWait < 999999; iWait++)
{
if (iWait % 100000 == 0)
cout << ".";
}
cout << endl << iListCount << " List Size" << endl;
cout << endl;
}
// End of Part 1 Draw
// Start of Sort
int iNumbersDrawn = 0;
for (int n = 1; n < 60; n++)
{
if (iNumberCount1[n]>0)
{
cout << " " << n << " Drawn " << iNumberCount1[n] << " times." << std::endl;
iNumbersDrawn = iNumbersDrawn + iNumberCount1[n];
}
}
int iHigh1 = 0;
int iHigh2 = 0;
int iHigh3 = 0;
int iHigh4 = 0;
int iHigh5 = 0;
int iHigh6 = 0;
int iLow1 = iMainLoopMax;
int iLow2 = iMainLoopMax;
int iLow3 = iMainLoopMax;
int iLow4 = iMainLoopMax;
int iLow5 = iMainLoopMax;
int iLow6 = iMainLoopMax;
string sHigh1 = "";
string sHigh2 = "";
string sHigh3 = "";
string sHigh4 = "";
string sHigh5 = "";
string sHigh6 = "";
string sLow1 = "";
string sLow2 = "";
string sLow3 = "";
string sLow4 = "";
string sLow5 = "";
string sLow6 = "";
cout << endl << "CALCULATING HIGH DRAWS" << endl;
for (int n = 1; n < 60; n++)
{
int y = generateRandomNumber();
int iExit = 0;
cout << ".";
if (iNumberCount1[y] != 0)
{
std::ostringstream convert;
convert << y;
if (iNumberCount1[y] >= iHigh1)
{
cout << endl << "INSERTING " << y << " @ Posistion 1" << endl;
iHigh6 = iHigh5;
iHigh5 = iHigh4;
iHigh4 = iHigh3;
iHigh3 = iHigh2;
iHigh2 = iHigh1;
iHigh1 = iNumberCount1[y];
sHigh6 = sHigh5;
sHigh5 = sHigh4;
sHigh4 = sHigh3;
sHigh3 = sHigh2;
sHigh2 = sHigh1;
sHigh1 = convert.str();
iExit = 1;
}
if (iExit == 0)
{
if (iNumberCount1[y] >= iHigh2)
{
cout << endl << "INSERTING " << y << " @ Posistion 2" << endl;
iHigh6 = iHigh5;
iHigh5 = iHigh4;
iHigh4 = iHigh3;
iHigh3 = iHigh2;
iHigh2 = iNumberCount1[y];
sHigh6 = sHigh5;
sHigh5 = sHigh4;
sHigh4 = sHigh3;
sHigh3 = sHigh2;
sHigh2 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount1[y] >= iHigh3)
{
cout << endl << "INSERTING " << y << " @ Posistion 3" << endl;
iHigh6 = iHigh5;
iHigh5 = iHigh4;
iHigh4 = iHigh3;
iHigh3 = iNumberCount1[y];
sHigh6 = sHigh5;
sHigh5 = sHigh4;
sHigh4 = sHigh3;
sHigh3 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount1[y] >= iHigh4)
{
cout << endl << "INSERTING " << y << " @ Posistion 4" << endl;
iHigh6 = iHigh5;
iHigh5 = iHigh4;
iHigh4 = iNumberCount1[y];
sHigh6 = sHigh5;
sHigh5 = sHigh4;
sHigh4 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount1[y] >= iHigh5)
{
cout << endl << "INSERTING " << y << " @ Posistion 5" << endl;
iHigh6 = iHigh5;
iHigh5 = iNumberCount1[y];
sHigh6 = sHigh5;
sHigh5 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount1[y] >= iHigh6)
{
cout << endl << "INSERTING " << y << " @ Posistion 6" << endl;
iHigh6 = iNumberCount1[y];
sHigh6 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
cout << endl << y << " NOT INSERTED" << endl;
}
iNumberCount1[y] = 0;
}
else
{
n = n - 1;
}
}
cout << endl << "CALCULATING LOW DRAWS" << endl;
for (int n = 1; n < 60; n++)
{
int y = generateRandomNumber();
int iExit = 0;
cout << ".";
if (iNumberCount2[y] != 0)
{
std::ostringstream convert;
convert << y;
if (iNumberCount2[y] <= iLow1)
{
cout << endl << "INSERTING " << y << " @ Posistion 1" << endl;
iLow6 = iLow5;
iLow5 = iLow4;
iLow4 = iLow3;
iLow3 = iLow2;
iLow2 = iLow1;
iLow1 = iNumberCount2[y];
sLow6 = sLow5;
sLow5 = sLow4;
sLow4 = sLow3;
sLow3 = sLow2;
sLow2 = sLow1;
sLow1 = convert.str();
iExit = 1;
}
if (iExit == 0)
{
if (iNumberCount2[y] <= iLow2)
{
cout << endl << "INSERTING " << y << " @ Posistion 2" << endl;
iLow6 = iLow5;
iLow5 = iLow4;
iLow4 = iLow3;
iLow3 = iLow2;
iLow2 = iNumberCount2[y];
sLow6 = sLow5;
sLow5 = sLow4;
sLow4 = sLow3;
sLow3 = sLow2;
sLow2 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount2[y] <= iLow3)
{
cout << endl << "INSERTING " << y << " @ Posistion 3" << endl;
iLow6 = iLow5;
iLow5 = iLow4;
iLow4 = iLow3;
iLow3 = iNumberCount2[y];
sLow6 = sLow5;
sLow5 = sLow4;
sLow4 = sLow3;
sLow3 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount2[y] <= iLow4)
{
cout << endl << "INSERTING " << y << " @ Posistion 4" << endl;
iLow6 = iLow5;
iLow5 = iLow4;
iLow4 = iNumberCount2[y];
sLow6 = sLow5;
sLow5 = sLow4;
sLow4 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount2[y] <= iLow5)
{
cout << endl << "INSERTING " << y << " @ Posistion 5" << endl;
iLow6 = iLow5;
iLow5 = iNumberCount2[y];
sLow6 = sLow5;
sLow5 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
if (iNumberCount2[y] <= iLow6)
{
cout << endl << "INSERTING " << y << " @ Posistion 6" << endl;
iLow6 = iNumberCount2[y];
sLow6 = convert.str();
iExit = 1;
}
}
if (iExit == 0)
{
cout << endl << y << " NOT INSERTED" << endl;
}
iNumberCount2[y] = 0;
}
else
{
n = n - 1;
}
}
cout << "\tThese are your recommended HIGH numbers: " << endl
<< "\t" << sHigh1 << " with " << iHigh1 << " draws" << endl
<< "\t" << sHigh2 << " with " << iHigh2 << " draws" << endl
<< "\t" << sHigh3 << " with " << iHigh3 << " draws" << endl
<< "\t" << sHigh4 << " with " << iHigh4 << " draws" << endl
<< "\t" << sHigh5 << " with " << iHigh5 << " draws" << endl
<< "\t" << sHigh6 << " with " << iHigh6 << " draws" << endl;
cout << "\tThese are your recommended LOW numbers: " << endl
<< "\t" << sLow1 << " with " << iLow1 << " draws" << endl
<< "\t" << sLow2 << " with " << iLow2 << " draws" << endl
<< "\t" << sLow3 << " with " << iLow3 << " draws" << endl
<< "\t" << sLow4 << " with " << iLow4 << " draws" << endl
<< "\t" << sLow5 << " with " << iLow5 << " draws" << endl
<< "\t" << sLow6 << " with " << iLow6 << " draws" << endl;
sExit = getchar();
while (sExit != "Q")
{
// Left in for Windows Console
}
return 0;
}