Random Number Generators Always Return The Same Value

I have been trying to get a random value between 0 and 2 inclusive using the many random number generators that the unreal engine provides. However every time I use one, it always produces the same number for every actor I spawn.

I am trying to conditionally set a different material for each of the actors I spawn, but the random number generators are all spitting out the same number.

However, when I tried using the Random Integer In Range blueprint node, that node could correctly generate random numbers.

In code I have:
RoomColor = FMath::RandRange(0, 2);

I have put this line in the constructor, BeginPlay, and Tick functions, but I have not gotten it to produce a different number other than 0.

Also, just to add: I have tried FRandomStreams and I have tried all FMath seeding methods both in the Actor’s constructor/BeginPlay functions and in the GameMode, to no avail.

Hello,

There is currently a bug in our database (UE-12306) related to random number generators having a higher probability of returning 0 than any other number. However, it sounds like you are saying that you were unable to get another number regardless of how many times you’ve tried. Would you mind providing the complete code that you are using to set this up so that we can test it on our end?

After doing some testing I no longer think there is an issue with the random number generators (other than what you specified in the bug report above).

I’m not sure why this is happening but for some reason, the int32 variable controlling the color of the room is being set to an arbitrary number (always the same) after correctly being set to a random number in the constructor.

Just as a test I am generating the random number in the constructor, which works correctly.
Then in the GameMode function where I spawn the actor, I call the actor’s Init function (a custom function), and print out the state of the previously randomly generated number, which now displays a different value.
Just to test again I print out the value once the BeginPlay function is called on the actor, which also displays the changed value.
Code for this can be seen below:
GameMode.cpp (See attached): GameMode.cpp

SingleCube.cpp (Actor) (See attached): SingleCube.cpp

Upon running this code I get the output in the attached file RoomColorOutput0.txt.
Then when I change the 6 in RandHelper to an 8 and recompile, I get the output in the attached file RoomColorOutput7.txt.
The number it seems to choose is never the same between compiles except for that it is between 0 and the number specified to RandHelper. It also doesnt seem to be the first or last number generated from the output.

Are you providing a randomized seed for your random number generator? If not, this would explain why the same number is constantly being generated.

I dont think this is an issue with the random number generator or seeding it as it is correctly generating random numbers in the constructors of each of the spawned actors as shown in the previously attached output files. Though I would be willing to test if seeding would help. Is there a specific method that will seed the FMath::RandHelper function?

What I think is happening is, at compile time, the SingleCube constructor is being called and statically setting the RoomColor variable (which is a dynamic variable which you can see in the attached SingleCube.h file). Then when the actor is spawned it calls the constructor again, but for some reason either references the static RoomColor variable or copies the value from the SingleCube object that was constructed on compile.

Hello,

I have managed to reproduce your issue, and have entered a bug report (UE-25397). Thank you for your report.

In the meantime, I was able to get the number to generate properly when using FMath::RandHelper(6) in BeginPlay instead of in the constructor. So if possible, I’d recommend doing it this way until this issue is resolved.

Have a great day

Thanks so much Sean! I’m a little new and I’m not sure where I can find the bug repository to search for the report. Could you point me in the right direction?

Ah alright. Thanks again! :smiley:

Currently we do not have a public bug tracker available, so this can only be referenced internally. We give out the bug IDs just for reference purposes. I will provide updates on the issue as they become available.

Your RoomColor property is marked as a UPROPERTY, which means it will be copied from the Class Default Object after the Constructor runs on the instance. I think this explains what you are seeing. Generating the random number in PostInitProperties rather than the constructor should resolve the issue.

That function definitely works. Though I think it might be better to use a custom Init function. As it allows for a custom parameter set to be passed in to initialize the object.

I think being able to spawn actors with a custom constructor (with custom parameters) might help this situation but I believe my problem is solved.

Thanks for looking into this!

Sean, why can I not find UE-12306 in the issues database?

Tickets need to be manually marked for public visibility, so some of them in our backlog have not yet been processed, and some tickets cannot be made visible at all due to sensitive information. However, I have gone ahead and made it public, so you should be able to find it now.

Here’s a link for your convenience: Unreal Engine Issues and Bug Tracker (UE-12306)