Random TCHAR from FString

So I was thinking about something, we have all these random number functions yet not a random letter generator and yeah it might be easier to make an ascii generator using numerical representation. but i have this function


FString UGeneticsComponent::RandomSymbol(){
    FString vaild = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`-=]\',./~!@#$%^&*()_+{}|:<>?";
    return (&vaild.GetCharArray()[FMath::RandRange(0, vaild.Len())]);
}

On the face it looks like it will return a single array index from that chararray, yet when printed, it prints from the random char to the end. for example

will print if ‘n’ is selected.

how can i print only a single letter from vaild fstring ?

edit -
i wonder if printing the [0] index of the result from (&vaild.GetCharArray()[FMath::RandRange(0, vaild.Len())])


FString vaild = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`-=]\',./~!@#$%^&*()_+{}|:<>?";
FString rtnStr = (&vaild.GetCharArray()[FMath::RandRange(0, vaild.Len())]);
return &rtnStr[0];

Still prints like the function above.

1 Like

That was an easy fix. don’t mind me, just leaving this here in case someone finds it useful.

Why not just use something like:



FString vaild = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890`-=]\',./~!@#$%^&*()_+{}|:<>?";
int32 randomIndex = FMath::RandRange(0,valid.Len());
return &valid[randomIndex];


2 Likes

That’s a fair assertion. I am really tired xD. I’m pretty sure actually if we use just len() though it will give us a potentially out of bound number. so len()-1 would work right because of the escape character ?

edit actually i think your code will also give you the problem above. idk though. i think the magic is in the *“FString(1, &rtnStr[0])” *because its only the first character in the array.

Perhaps, I definitely didn’t confirm the code would function - More of a suggestion.

*Me either, all the code i have posted so far is theoretically correct.

Utilizing ASCII-characters would be a much easier solution if you ask me.

This function should be able to do what you want, and more.



FString GenerateRandomCharacters(uint32 AmountOfCharacters) {
FString Result = "";
for (uint32 = 0; i < AmountOfCharacters; ++i) {
Result += FMath::RandRange(0, 127);
}
return Result;
}


Reference:
http://www.bibase.com/images/ascii.gif

Haven’t tried it out, but it should work :slight_smile:

2 Likes

Hey this is cool! Looks really nice.

Granted you’ll want to change the min to 32 as the first 32 entries are non displayable entries.

but yeah awesome stuff :slight_smile: ill test it here shortly.

edit : tried your function, i made a tiny change

Result += FMath::RandRange(33, 126);

since, 0 - 31 will give you non displayables, 32 is space, if you want spaces change 33 to 32, but i also got rid of potential deletes too, but yes, this is a much better function. :smiley: thanks.

1 Like

Oh, right!
Glad it works :smiley:
Good luck with your work!

Beware of RandRange can include both extremes.
int32 randomIndex = FMath::RandRange(0,valid.Len()-1);