Hey guys, I'm new to C++ and in desperate need of help. I'm trying to write a program that is like powerball. I need to generate six random numbers. The first five numbers need to be in the range of 1-55 while the sixth number is in the range of 1-42. The program needs no input from the user and should generate random numbers in those ranges everytime on startup. Also, the header which is just "Powerball", and all six identical random numbers need to be written to text file named "Powerball.txt which should be located in the solution folder of the Visual Studios Project. I'm new to both random numbers and input/outfile and I just can't seem to figure it out. Any help is well appreciated as I need to complete this asap. Thank you.

Dani AI

Generated

A few practical clarifications and a compact, more robust example that follows the requirements in the thread.

The snippet posted by shows the common srand/time + rand()%N approach; that works for quick tests but has a few pitfalls: seeding repeatedly or with one-second resolution can produce repeated runs with the same sequence, rand() % N has small modulo bias, and system("pause") and using namespace std; are worth avoiding. 's suggestion to add two lines for the sixth ball is fine for a quick fix, but a closer match to real Powerball behavior is to (a) ensure the five white balls are unique and (b) write the same header and numbers both to the console and to Powerball.txt. Visual Studio will create the file in the process working directory; to place it in the solution folder either set Project Properties -> Debugging -> Working Directory to $(SolutionDir) or open an explicit path.

A concise C++11+ solution (writes identical output to console and Powerball.txt, enforces unique white balls 1-55 and a red ball 1-42):

#include <iostream>
#include <fstream>
#include <vector>
#include <random>
#include <algorithm>

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> white(1,55);
    std::uniform_int_distribution<int> red(1,42);

    std::vector<int> whites;
    while (whites.size() < 5) {
        int n = white(gen);
        if (std::find(whites.begin(), whites.end(), n) == whites.end())
            whites.push_back(n);
    }
    std::sort(whites.begin(), whites.end());
    int power = red(gen);

    std::ofstream ofs("Powerball.txt");
    if (!ofs) return 1;

    ofs << "Powerball\n";
    std::cout << "Powerball\n";
    for (int v : whites) { ofs << v << ' '; std::cout << v << ' '; }
    ofs << "| " << power << '\n';
    std::cout << "| " << power << '\n';
}

If compiling with a pre-C++11 compiler, keep the single srand(...) seed (once, outside loops), avoid system("pause"), check ofstream success, and explicitly guard for duplicate white numbers. was right to ask for posted attempts — showing an attempt (even partial) helps focus suggestions.

Recommended Answers

All 4 Replies

If you read the sticky posts in this forum, you'll see we don't do your work for you. Please show the work you've done so far, and then we'll help point you in the right direction.

I actually found this site while trying to fix my awful coding so no, I did not have a chance to read the site's stickies and for that, I apologize. I've been trying to figure this out for the past three hours and I know this is pretty simple but I just started on my own last week. I took out the coding for the in/out file as it was just causing a headache(sintax errors).

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    for(int index=0; index<5; index++){ 
        random_integer = (rand()%55)+1; 
        cout << random_integer << endl;
	}
		
		system ("pause");
}

I have my first five random numbers down but the sixth is where I'm confused as it involves a different range 1-42. I'm not sure how to incorporate the in/output coding either. "Starting out with C++" by Tony Gaddis is kind of brief on those functions...

I actually found this site while trying to fix my awful coding so no, I did not have a chance to read the site's stickies and for that, I apologize. I've been trying to figure this out for the past three hours and I know this is pretty simple but I just started on my own last week. I took out the coding for the in/out file as it was just causing a headache(sintax errors).

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
    int random_integer; 
    for(int index=0; index<5; index++){ 
        random_integer = (rand()%55)+1; 
        cout << random_integer << endl;
	}
		
		system ("pause");
}

I have my first five random numbers down but the sixth is where I'm confused as it involves a different range 1-42. I'm not sure how to incorporate the in/output coding either. "Starting out with C++" by Tony Gaddis is kind of brief on those functions...

Perhaps add another two lines of code after the 'for' loop?

random_integer = (rand()%42)+1; 
        cout << random_integer << endl;

I was up all night and finally figured it out. Although the code doesn't look so pretty and it's some what longer then needed, it works. Thanks for the response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.