2

I am wondering how to randomly generate a string per iteration. My code currently generates the same string each iteration. If I type in 3 times, then it will give me the same string 3 times. I want a different and randomly generated string each time.

#include <iostream>
#include <string>
#include <cstdlib>     /* srand, rand */
#include <ctime>
using namespace std;

string RandomString(int len)
{
   string str = "0123456789ABCDEFabcdef";
   string newstr;
   int pos;
   while(newstr.size() != len) {
    pos = ((rand() % (str.size() - 1)));
    newstr += str.substr(pos,1);
   }
   return newstr;
}

int main()
{
   srand(time(NULL));
   string random_str = RandomString(32);
   int user_input;
   
   cout << "Enter how many codes you want: ";
   cin >> user_input;
   for (int i = 0; i < user_input; i++)
   {
   cout << "random_str : " << random_str << endl;
   }

}

Enter how many codes you want: 3 random_str : ae2e8D6C7C04Fb3b83Ec457bcedcC5F5 random_str : ae2e8D6C7C04Fb3b83Ec457bcedcC5F5 random_str : ae2e8D6C7C04Fb3b83Ec457bcedcC5F5

This is my current output. Remember, they all should be different each time.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • 2
    You only call `RandomString()` once. – Fred Larson Apr 07 '21 at 22:35
  • 1
    Indeed, you're printing the same string from `main()`. Have you considered calling `RandomString()` on _each_ loop iteration in `main()`? – bipll Apr 07 '21 at 22:36
  • How many times to do you need to call `str.size()`? Does it change? You could assign it to a `const unsigned int` before the loop. – Thomas Matthews Apr 07 '21 at 22:38
  • 1
    Thank you guys very much for correcting my mistake. You guys did good and helped. – DarknessArise03 Apr 07 '21 at 22:38
  • 1
    @DarknessArise03 If you're more curious, [using the string fuzzer in this answer](https://stackoverflow.com/questions/63046559/code-submission-on-spoj-gives-runtime-error-sigabrt/63048464#63048464), this code [here](http://coliru.stacked-crooked.com/a/02b05f0c8f872c8a) does what you are looking for and a whole lot more. If you take a look at it, it uses your character set, it uses your minimum and max length (32), and it generates 1 random string for each iteration of the for loop. – PaulMcKenzie Apr 07 '21 at 22:52
  • 1
    Unrelated: [`std::shuffle`](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) could save you a bunch of code. – user4581301 Apr 07 '21 at 23:16
  • 1
    I think you should definitely look at the links @PaulMcKenzie provided. Mainly because they show the better way to generate random numbers/strings/etc, instead of `rand() % n`. For info why it's discouraged to use rand(), see [here](https://stackoverflow.com/a/52870307/11585371) or [this entertaining presentation](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) – MassiveAtoms Apr 08 '21 at 00:27

1 Answers1

3

You need to call the function at every iteration, place it inside the for loop:

int main()
{
    srand(time(NULL));

    int user_input;

    cout << "Enter how many codes you want: ";
    cin >> user_input;
    
    for (int i = 0; i < user_input; i++)
    {
        string random_str = RandomString(32); //<-- here
        cout << "random_str : " << random_str << endl;
    }
}

Using C++ you do have much better random numbers engines, take a look here:

https://en.cppreference.com/w/cpp/numeric/random

anastaciu
  • 23,467
  • 7
  • 28
  • 53