First of all this is my first time posting here so sorry if I made any mistakes with the title/format. Ok, so I have to make a funcion that returns a random character from a string. I tried making it this way:
include <string>
#include "charset.h"
#include <stdlib.h>
#include <time.h>
namespace Components {
std::string charSet= " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,.;:-_";
char randChar (){
srand(time(NULL));
uint a = charSet.length();
int m= rand()%a;
return charSet[m];
}
}
`
And for some reason it always returns the same char, every time I run the code it generates a new random one and just returns that same one everytime until I stop it and run it again. I thought srand(time(NULL)) was suposed to fix that but it keeps happening. I've tried with random() / srandom(time(NULL)) and they give the same result.
Again, sorry if I made anything wrong or this was alreayd posted somewhere, thanks for any help!
Aucun commentaire:
Enregistrer un commentaire