-2

This might be ridiculously easy but I'm still beginner. I would need to create a string including 5 random numbers seperated by spaces.

With code below, I get e.g. random = "12345" but I need string random = "1 2 3 4 5" in the end of the code because later I need to use these numbers as a string in istringstream is { random } command.

So to sum up, I need to create string including 5 random numbers seperated by spaces.

Thanks for help in advance.

int main()
{
        cout <<"Enter seed value: ";
        int seed;
        cin >> seed;

    srand(seed); // random number generator uses seed value entered by the user

    for (int i = 1; i <= 5; i++) { // print 25 numbers
        int random = 1 + (rand() % 5); // numbers are between 1 - 5

        cout << random ;
    }
    return 1;
}
  • `cout << random << ' ';`, if you just want the output. – Some programmer dude Aug 12 '22 at 07:13
  • `std::ostringstream` might come in handy to realize that. – πάντα ῥεῖ Aug 12 '22 at 07:13
  • And if you want a string for later, you do know about `std::istringstream`, but haven't you thought about `std::ostringstream` to "output" the numbers? – Some programmer dude Aug 12 '22 at 07:14
  • I don't want just a output, I need to get random = "1 2 3 4 5" – c_plusplus_beginner Aug 12 '22 at 07:15
  • 1
    On another note, the C++ standard library have [many facilities for generating random numbers](https://en.cppreference.com/w/cpp/numeric/random), most better than plain `srand` and `rand`. For example you could use [`std::uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) to get a nice evenly distribution of a range of numbers. Or create an array of five elements, initialized from `1` to `5` (inclusive) and then just [shuffle](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) it (if you need all the numbers, just in a random order)? – Some programmer dude Aug 12 '22 at 07:16
  • 1
    While it's not relevant here on side note, it's nearly never good idea to use modulo on a random value because it introduces a bias. And you wrote that you're using C++, but `rand()` function is a C legacy, there are better generators in the C++ library, – Swift - Friday Pie Aug 12 '22 at 07:33
  • By the way, if you later need the string in an `std::istringstream`, why not use the in *and* out `std::stringstream`? Then you only need one stream object for the creation and extraction of the data. And why do you even need an `istringstream`? To extract the random numbers later (as strings or integers)? Why not just store the numbers in a vector or array without the need of any string streams whatsoever? – Some programmer dude Aug 12 '22 at 07:44
  • thanks for all the comments! I got problem solved. – c_plusplus_beginner Aug 12 '22 at 09:05

2 Answers2

0

Just as you need to string for an istringstream later you can use an ostringstream to create the string in the first place.

ostringstream oss;
for (int i = 0; i < 5; ++i)
{
    if (i > 0)     // if not the first number add a space between the numbers
        oss << ' ';
    oss << 1 + (rand() % 5); // add the random number
}
string randomString = oss.str(); // get the string out of the ostringstream
john
  • 85,011
  • 4
  • 57
  • 81
0

You can print the space too after printing the random number, if printing is what you want

int last = 5;
for (int i = 1; i <= last; i++) { // print 25 numbers
    int random = 1 + (rand() % 5); // numbers are between 1 - 5
    cout << random ;
    if( i != last ) cout << " ";
}