Random not random

Hello
I have a small problem with this part of the code:

In the editor, when testing on editor, i don’t have any problem on random but after building the random is not random.

Because random in computers is really not random :stuck_out_tongue: CPU can’t generate random numbers, insted OS or application use battlefly effect kind of argorithms to generate random numbers based on input (seed), but if you use same input it will generate same stream of numbers. So my suspect is that both functions you use are using same seed and generate same numbers.

There other version of random nodes where you can input custom random stream based on custom seed, dont rember the exact name but search in libery “random” and you should find it, there also node that generate random stream which you will need to use.

Yes i know random isn’t really random but when i simulate orplay in editor, the number change every time.
Why when its build, the number stay the same (in my case now 41) and try with other random in other blueprint and same.
Random working on editor but not really on the builded version.

I have try random stream and same result, very strange i have no problem on editor and but only in binnarie.

Thanks for help me, i m really disapointed.

I have the exact same problem with the Shuffle node on an array. See: Shuffle node doesn't work after shipping - Programming & Scripting - Unreal Engine Forums

Make sure you use a different Random Node for each different random generation you need. If you use one node and plug it into multiple other nodes then you run the risk of getting the same random number applied to all of them.

I even created a function in C++ to generate values, and made sure (using debug in Visual Studio), that I got new values everytime. Still, after packing the project and running in Windows, the numbers stay the same. I believe this must be a memory/garbage collection issue, or perhaps there is a proper way of cleaning up variables and such in Blueprint/C++ before the game exists?

Also tried using all kinds of combinations for blueprints using arrays, variables etc. Problem remains, the pseudorandom generators work, but only the first after packaging. After that the results seem “cached”.

Did anyone find a solution for random not being random?

Same issue here. I’ve tried every seeding trick I can think of, but in packaged builds random functions (frandinrange, frandinrangefromstream, etc) produce the same numbers every time. I found another thread which suggested seeding with srand(time(null)) [as the unreal random wrappers actually use srand underneath] but this didn’t work for me either.

Did anyone get this to work yet?

PS: Everything works perfectly in the editor of course, only packaged builds are affected.

Okay, after several hours of troubleshooting I finally got random to work in packaged builds.

The basic idea is described in this thread thanks to RickDangerous, it took me a while to get it working though.

It looks like for packaged builds the seed used in FMath based random functions (which are just wrappers around the standard C++ rand/srand functions) gets set to some constant value each time you package the game. I thought the fix would be as simple as calling srand(Time(NULL)) from event begin play of my gamemode but I found that the seed gets reset to a constant value so reseeding it in event begin play is too early it seems.

The workaround I finally used was to call srand(Time(NULL)) from ‘InitializeComponent’ event of my component that needed random behavior, but I had to ensure that this is called once and only once - you do not want every single instance of that component to be resetting the seed repeatedly.

Frankly I wish the behavior of random functions in packaged games and editor mode would be consistent, but for now I’m happy with this technique/workaround and it is giving me reasonable results.

Any tips on how to fix this with blueprints?

my project currently does all of the random events inside of BPs… if not, is there some piece of code I could add that would work for all of those functions without having to recreate them?

You shouldn’t have to rewrite your BP random events - however you do need to call srand(Time(NULL)) just once from somewhere in C++.

Off the top of my head this is the solution that I used:

  1. Create a C++ singleton with a boolean variable that tracks whether you have already reset the seed.
  2. Find an appropriate place (any of your main classes) to call srand(Time(NULL)) and set the singleton boolean to true to ensure no other component calls it again. For mine I used an InitializeComponent event of one of my actors.

It could be simpler yet - creating a custom gamemode (File->new C++ class → derive from AGameMode), implementing event begin play and sticking srand(Time(NULL)) in there should work too but this did not work for me if I remember correctly.