Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unique numbers using srand( ) and rand( ) functions in C++

Reply
Thread Tools

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

 
 
August1
Guest
Posts: n/a
 
      05-16-2004
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: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[i]=(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[i]) temp++;
if(temp==0) i++;
}

for(i=0;i<6;i++)
{
send2file << numArr[i];
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()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
unique numbers using srand( ) and rand( ) functions in C++ August1 C++ 0 05-16-2004 08:01 AM
unique numbers using srand( ) and rand( ) functions in C++ August1 C++ 4 12-08-2003 03:47 PM



Advertisments
 



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