0

So, I wanted to create code, that generates certain amount of random strings and prints it in file (test.txt). But those strings, generated with rand(), just repeat in this file. How can I deal with this?

using namespace std;

 string h(){
    
      srand (time(NULL));
    int a=rand()%7+4;
    
string c;

    for(int i=0;i<a;i++){
    
        char b=rand()%123;
        
    c+=b;
        }
    return c;
 }
 int main(){
    
    srand (time(NULL));
    
    int a;
    
     cin>>a; // here we choose amount of strings in future file
    
    ofstream n("test.txt");//opening file to print strings
    
    for(int i=0;i<a;i++){
        
            n << h()<<endl; 
                
    }
    n.close();
 }```
Dioo0_
  • 1
  • 1
    Remove `srand (time(NULL));` from the function `h`. Otherwise, there is a high chance that same seed value is used and same random sequence is generated because the execution of the function will take much less than one second. – MikeCAT Jun 23 '21 at 16:18
  • thanks, already fixed – Dioo0_ Jun 24 '21 at 13:45

0 Answers0