Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8047005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:58:08+00:00 2026-06-05T05:58:08+00:00

I basically, for this assignment, have to make a mini-program that randomly generates a

  • 0

I basically, for this assignment, have to make a mini-program that randomly generates a string of letters based on randomized numbers (for which 0 corresponds to A, 1 corresponds to B, 2 corresponds to C, and so on) alongside with a number n corresponding to the nth string generated. These strings and their corresponding numbering are to be printed out to a text file. For example, if my function was called myFunction:

myFunction("file.txt", 3)

Produces a text file called “file.txt” with the following contents:

AAAAAAAA 1
IWFNWEFS 2
WEWEFCSD 3

Where the strings’ letters are each supposed to be randomized.

However, for some reason each of my strings are identical for some reason. That is, for each row of string-number pairs, all the strings turn out to be the same (when I am supposed to make them all different/random). Why is this happening? Here is my code:

#include <iostream>
#include <vector>
#include <cstdlib>
#include "math.h"
#include <fstream>
using namespace std;

string key = ""; // Empty initial key for use in "makeKey".

// "makeKey" function to create an alphabetical key 
// based on 8 randomized numbers 0 - 25.
void makeKey() {
    int k;
    for (k = 0; k < 8; k++) {
        int keyNumber = (rand() % 25);
        if (keyNumber == 0)
            key.append("A");
        if (keyNumber == 1)
            key.append("B");
        if (keyNumber == 2)
            key.append("C");
        if (keyNumber == 3)
            key.append("D");
        if (keyNumber == 4)
            key.append("E");
        if (keyNumber == 5)
            key.append("F");
        if (keyNumber == 6)
            key.append("G");
        if (keyNumber == 7)
            key.append("H");
        if (keyNumber == 8)
            key.append("I");
        if (keyNumber == 9)
            key.append("J");
        if (keyNumber == 10)
            key.append("K");
        if (keyNumber == 11)
            key.append("L");
        if (keyNumber == 12)
            key.append("M");
        if (keyNumber == 13)
            key.append("N");
        if (keyNumber == 14)
            key.append("O");
        if (keyNumber == 15)
            key.append("P");
        if (keyNumber == 16)
            key.append("Q");
        if (keyNumber == 17)
            key.append("R");
        if (keyNumber == 18)
            key.append("S");
        if (keyNumber == 19)
            key.append("T");
        if (keyNumber == 20)
            key.append("U");
        if (keyNumber == 21)
            key.append("V");
        if (keyNumber == 22)
            key.append("W");
        if (keyNumber == 23)
            key.append("X");
        if (keyNumber == 24)
            key.append("Y");
        if (keyNumber == 25)
            key.append("Z");
    }
    return;
}

// "makeFile" function to produce the desired text file.
// Note this only works as intended if you include the ".txt" extension,
// and that a file of the same name doesn't already exist.
void makeFile(string fileName, int n) {
    ofstream ourFile;
    ourFile.open(fileName);
    int k; // For use in below loop to compare with n.
    int l; // For use in the loop inside the below loop.
    for (k = 1; k <= n; k++) {
        for (l = 0; l < 8; l++) {  // To write to the file ONE key
        ourFile << key[l];         // C++ only lets me do it this way...
        }
        ourFile << "  " << k << "\n"; // Writes two spaces and the data value
    }
}

// Primary function to create our desired file!
void mainFunction(string fileName, int n) {
    makeKey();
    makeFile(fileName, n);
}

int main() {
    mainFunction("file.txt", 3); // To test program
    cin.get();
    return 0;
}

I tried using both srand ( time(NULL) ); and srand (1); in the first part, but that didn’t work for me either.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:58:11+00:00Added an answer on June 5, 2026 at 5:58 am

    Each time you call makeKey, it appends 8 more characters to the end of key, but you never clear out the previous contents, and after calling makeKey, you print out the first 8 characters, which haven’t changed.

    I’d get rid of the global variable key, and have makeKey return a (newly created) std::string every time it’s called.

    FWIW, I’d also rewrite makeKey to convert from numbers to letters by indexing into an array of char.

    Edit (In response to the edit in the question): Yes, you probably also want to use srand(time(NULL)); at the beginning of the program. srand(1); is the same as if you didn’t bother to call srand at all. Using srand(time(NULL)); should normally give a different sequence of results every time you run the program (unless you run it twice so quickly that the system clock doesn’t change in between — normally twice in the same second).

    Right now you’re getting one string repeated 8 times. Fixing the problem noted above will give you 8 different strings — but without something like srand(time(NULL));, you’ll get the same group of 8 strings ever time you run the program. If you just add srand(time(NULL)); and don’t fix the rest, you’ll get one string 8 times, but it’ll be a different one each time you run the program (whereas right now, even the one string you get will be the same as every other time you run the program).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
I have an assignment to find the minimum, maximum, and average of numbers that
I have this school assignment that I've been working on and am totally stumped
Basically, I have an assignment that requires me to find the mode of a
I have a simple assignment that the professor wants us to do. Basically to
So i have been creating this framework thing that basically puts together source code
The problem is basically this, in python's gobject and gtk bindings. Assume we have
My understanding is that Ladner's theorem is basically this: P != NP implies that
This maybe a related question: Java assignment issues - Is this atomic? I have
So basically I am trying to make a model loader, that will take in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.