unique numbers using srand( ) and rand( ) functions in C++

A

August1

This is a follow up to using these functions to produce lottery
numbers to an outfile, then read the numbers from the file to the
screen. Although other methods are certainly available.

#include <iostream>
#include <fstream>//for declaring objects of the ofstream and ifstream
#include <time.h>
#include <stdlib.h>
using namespace std;

#define MAX 54//defines the upper bound range of array numbers as 54

void main(void)
{
//declare integer array and variable types
int numArr[6],temp,randomNum;
short numero = 0;
short b = 0;

//Plant seed for random number generator with system time
srand((unsigned int)time(NULL)/2);

ofstream send2file;//declare object of ofstream class for use with
file
ifstream readfile;//declare object of ifstream class for use with file
send2file.open("T8Be08b.dat", ios::eek:ut);/*use open function

//heading and information
cout << "\t\t\tLottery Numbers" << endl << endl
<< "This program opens the data file \"T8Be08b.dat\" and then stores
groups of" << endl
<< "lotto numbers that have 6 unique numbers to a group in the data
file." << endl
<< "There are 50 groups of numbers. The groups of numbers are
displayed to the" << endl
<< "screen - 3 groups per line." << endl << endl;

if(!send2file.fail())//if opening data file was successful
{

//Loop to repeat the sequence 50 times for each Lotto group
for(int x=0;x<50;x++)
{
//Loop for the six Lotto numbers
for(int i=0;i<6;)
{
randomNum=rand(); //assigns random # from 0-32767

//Converts to 1-MAX (upperbound limit)
numArr=(randomNum-(((int)(((float)randomNum)/((float)MAX)))*MAX))+1;

//Loop to check each number against the others
for(int num=temp=0;num<i;num++)
if(numArr[num]==numArr) temp++;
if(temp==0) i++;
}

for(i=0;i<6;i++)
{
send2file << numArr;
if(i<5)
{
send2file << "#";
}
if(i==5)//allows spacing between groups of 6 numbers
{
send2file << endl;
}
}//end for
}//end for

send2file.close();//use close function to close outfile

}
else
{
cout << "Error opening data file." << endl;
}

/////////////////////////////********************///////////////////////////////
readfile.open("T8Be08b.dat", ios::in);//open data file for input
if(readfile.fail())
{
cout << "Error opening data file." << endl;
}
else
{
for(short z = 0; z <= 5; z++)//read 1st record from the data file
{
readfile >> numArr[z];
readfile.ignore(1);/*consume # character between numbers of each
record in file
cout << numArr[z] << " ";//display each number in a group (6 per
group) }//end nested for() loop that reads 1st group of numbers from
data file

cout << " ";//put spacing between each group of numbers displayed
numero = 1;
while(b != 49)// records from the data file (50 in all)
{
for(short z = 0; z <= 5; z++)
{
readfile >> numArr[z];
readfile.ignore(1);
cout << numArr[z] << " ";
if(z == 5)
{
cout << " ";
}//end nested if
}//end nested for() loop
numero++;//increment counter for screen formatting
//if 3 groups of lotto numbers are on the output screen, go to next
line
if(numero == 3)
{
cout << endl;
numero = 0;//reset counter that allows 3 groups of numbers to appear
per line
}
b++;/*increment counter up to 50 for the 50 groups of lotto numbers
to be read from the data file*/
}//end nested while() loop
}//end if-else

cout << endl;//spacing

}//end main()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top