How to return random float concat_StrStr using C++?

Hi, I can achieve this using blueprint library but how to do it not using blueprint Library?

void AMyClass::GenerateUniqueNumber(FString& UniqueNumber)
{
	float RandomFloat_1{};
	FString RandomFloat_1_ToString{};
	RandomFloat_1 = UKismetMathLibrary::RandomFloat();
	RandomFloat_1_ToString = UKismetStringLibrary::Conv_FloatToString(RandomFloat_1);
	
	float RandomFloat_2{};
	FString RandomFloat_2_ToString{};
	RandomFloat_2 = UKismetMathLibrary::RandomFloat();
	RandomFloat_2_ToString = UKismetStringLibrary::Conv_FloatToString(RandomFloat_2);
	
	FString Concat_Return{};
	Concat_Return = UKismetStringLibrary::Concat_StrStr(RandomFloat_2_ToString, RandomFloat_1_ToString);
	UniqueNumber = Concat_Return;
}

You shouldn’t need KismetMathLibrary for this (Although you can use it)

Look into FMath::

To turn a float to string FString::SanitizeFloat(InFloat)

1 Like


I don’t know what range returning the randomfloat node, using FMath I will need to define the static range which is not I want.

KISMET_MATH_FORCEINLINE
float UKismetMathLibrary::RandomFloat()
{
	return FMath::FRand();
}
This is your whole function

UniqueNumber = FString::SanitizeFloat(FMath::FRand()) + FString::SanitizeFloat(FMath::FRand())

2 Likes

Thank You very much for this solution, and still I have a question what values are sums by FRand() if this is possible to know or they are taken from memory the random numbers?

I’m not sure I understand your question.

To be more specific lets look at the code

void AMyClass::GenerateUniqueNumber(FString& UniqueNumber)
{
  UniqueNumber= FString::SanitizeFloat(FMath::FRand()) + FString::SanitizeFloat(FMath::FRand());
}

it will return a random string, so on what float range FRand function is randomizing the floats? since It don’t need us to specify a range of floats.

or I am not doing it right and it need a float value like SanitizeFloat(FMath::FRand(MyFloatVariable)) ?

static FORCEINLINE float FRand() 
	{ 
		// FP32 mantissa can only represent 24 bits before losing precision
		constexpr int32 RandMax = 0x00ffffff < RAND_MAX ? 0x00ffffff : RAND_MAX;
		return (Rand() & RandMax) / (float)RandMax;
	}

It will be the same as your blueprint since it’s calling essentially this function anyways. But it’s a number 0 to MAX where MAX is the largest value representable by an FP32 that the system allows. MAX is never lower than 32767 but as high as 0x00ffffff

Max is also determined at compile time using constexpr so you will know the MAX during compile time.

1 Like

amazing information, this is what exactly I wanted know, Thank You very much Sir for sharing this information , it helps very much :slightly_smiling_face:

1 Like