How to Random integer in Range? C++

Hi, I am trying to do something like this without using kismet library.

void ATestActor::RetRandomNumber(int32& Number)
{
    TArray<int32> Arr{};
	int32 RandomInt{};
	int32 Array_Get_Item{};

    RandomInt = UKismetMathLibrary::RandomIntegerInRange(0, 99);
    FCustomThunkTemplates::Array_Get(Arr, RandomInt, Array_Get_Item);
    Number = Array_Get_Item;
}

Thank you

2 Likes

Dude, please…
ue4 c++ random int - Google Search

2 Likes

Sir I have already searched and found many answers but not the solution to my problem

FMath::RandRange(0, 99);
It’s literally in the first result.

Same with array:
Arr[FMath::RandRange(0, 99)];

3 Likes

and how to get (a copy) of the item founds under the range and set to Number?

Arr[i] already gets a copy of the element, you don’t need any separate function for that. And Number is just a return value, you don’t need a separate variable for that either.

The function is probably something like:
.h:
int32 GetRandomNumber();

.cpp:

int32 ATestActor::GetRandomNumber()
{
   return Arr[FMath::RandRange(0, 99)];
}

and when you need this random number, just call this function:

int32 NewRandomNumber = GetRandomNumber();

This, of course, if Arr is declared in .h so you can access it in the function’s namespace.

2 Likes

so in my case I can do something like this?
Number = Arr[FMath::RandRange(0, 99)]; under RetRandomNumber(int32& Number) { }

You can, but why?
Why use an Out Parameter when you can have a return type? So instead of something like:

int32 Number;
GetRandomNumber(Number);

You can simply do:
int32 Number = GetRandomNumber();

or even:
SomeOtherFunction(GetRandomNumber());

You can’t do the last two with an out parameter. Out parameters are useful when you need more than one variable to be changed by the function; when there’s only one, use a return type.

3 Likes

very informative, and I have a question about these two nodes,

what is supposed to be used by C++ version, refference or a copy version?
in blueprint I am using the copy version to avoid propagating any changes to the array itself.
image_2022-08-04_032711145

Have you tried getting a ref from the array and changing it in BP? It doesn’t change the array element. I guess in BP it always gets a copy unless it’s the array of pointers.

In cpp, if you don’t do anything specific to get a reference, [i] gets a copy. Unless it’s an array of pointers.

2 Likes

Thank you sir very much solving the issue, really appreciated this all expensive information and your time )

TArray<int32> Arr = { 0, 1, 2 };

	int32& ByRef = Arr[0]; //By ref

	int32 ByCopy = Arr[0]; //Getting a copy
2 Likes

in my case i have

ReturnRandomNumber(int32& Number) //By ref ? and should be (int32 Number) to make it //Getting a copy ?
 {
    TArray<int32> Arr{};
    Number = Arr[FMath::RandRange(0, 99)];  // Number is By ref returning the output?
 }

maybe this way is correct?

ReturnRandomNumber(int32& Number) 
 {
    TArray<int32> Arr{};
    int32 ByCopy = Arr[FMath::RandRange(0, 99)];
    Number = ByCopy;
 }
1 Like

In this case Arr and ByCopy are temporary variables so you don’t want to return the address of invalid memory, you should pass the array you’re working with or access it if it’s available.

You can have it defined in your header or available before calling the function, for this I would recommend returning a pointer so you can check for valid index

int32* ACodeFiveCharacter::GetArrByRef(TArray<int32>& ArrRef, int32 index) 
{
	if(ArrRef.IsValidIndex(index))
	{
		return &ArrRef[index];
	}

	return nullptr;
}
TArray<int32> Arr = { 0, 1, 2 };
	int32* ArrElem = GetArrByRef(Arr, 0); //Returns a pointer, can be null if index is not valid
	int32& ArrElemRef = *ArrElem; //If you want to work with ref variable but be sure ArrElem is not null.
2 Likes

very informative, thank you very much :slight_smile: for sharing the expensive information , I will be opening another thread if something goes wrong with my algorithm …

1 Like