Random numbers in Blueprint

It looks like you might be using this incorrectly. You only change the seed of a random number generator to alter the sequence of “Random” values that are produced by the stream. You only have to set the seed one time unless you want to alter the sequence in some way. Each blueprint sets a unique seed value by default, so you don’t have to set it like this.

Check out the docs here: Random Streams

In Blueprint, all you have to do is create a new variable of type RandomStream and then “Get” it and pull data from it. There are several conversion nodes to use, i.e. Random Bool from Stream, Random Vector from Stream… etc.

Just take a quick look over the Documentation page and that will sort you out real quick. =)

For a C++ project, it’s easy to generate a pseudo-random seed based on time. But my Blueprint-only project, once packaged, generates the same number (it does yield a random number in the editor but not from a .exe).

I’ve tried various BP configurations, assuming that a time function could help but there is also this notion of Random Stream which eludes me. I plugged the RandomStream with a New Seed of 0 or using the same Seconds (as below) with the same results.

Any help appreciated.

I tried your configuration and it works for me, printing between 0-2 (look like it’s flooring the float result so you practically never gonna have Max value)

Ah ok nvm then

Just a quick check : did you compile it and run it as an executable ? It works in the editor but not as a binary.

Thank you for the reference. Yeah, I should have thought to check the docs, too :slight_smile: it works great: again, in the editor: I see the new seed being generated each time in the tool tip over the blue object, I am not overriding it with a specific value. It works. But when I compile it in shipping mode to Windows, with the official 4.1.1 binary, I see the same numbers popping up.

The Random Integer node prints values [0 - Max)… in other words, [0 - (Max - 1)]. Also, there is no float result as you are using a Random Integer node. It produces integer results =)

I accidentally deleted my comment when I was trying to edit a typo… here it is again.

I don’t understand why you are resetting your Random Stream seed if you aren’t using your Random Stream to generate the random value. I said to check out the docs because you are using a Random Integer node to generate your pseudo-random value between 0 and 2 but you are not using the stream.

Take a look here:

I’m going to end the thread here. I’m not arguing your solution and you answered my fumbling with the random stream thing correctly. And your code works fine, its pretty much the same one I created after reading the docs.

I’m still having problem with the seed, whether I run the program multiple times in the editor, it gives me a different seed, hence a different set of numbers every time I run the program (Which is good). But when I compile the code in shipping mode Win64, running the program multiples times gives me the same set of numbers (Which is bad). So I guess I need to do more homework and build a better proof of concept before submitting a bug report. Who knows, the fix is probably addressed in 4.2.

Thanks again.

Hi, I am on 4.4.3 and I have the exact same issue as you: works in the editor but not in packaging. Did you ever find a solution? Any help would be greatly appreciated.

To all who have ever had this problem - you are right - it does return the same sequence when shipped, but not in the editor. I’m not sure whether it’s a bug or a feature (my mathematics and IT knowledge is very poor), but here’s one example - the Black Jack game provided by Epic, when launched as an executable (but not in the editor), returns the same “random” sequence of cards all the time depending on your bet and number of decks.

If you don’t believe me, here’s a David Copperfield-style trick for you - Open BlackJack (it seems the version doesn’t matter here - I’ve tried 4.2, 4.5., 4.6), package the project, run the .exe, choose “Number of decks” 1 and bet $25.
I know what you have - 9 and 4 of Spades! Fascinating, isn’t it? Not really. But wait, it gets better - exit the game, choose 5 decks and bet $5. It’s broken, I’m telling you.
Here is your proof:

It can only mean three things:

  1. Epic knew about it and didn’t intend this project to be shipped.
  2. Epic knew about it and didn’t intend the cards to be random. (but then, why is there this bug with 2 of Clubs?)
  3. Epic didn’t know about it.

Anyway, I got a bit carried away here. I guess that the seed really is the same all the time, so if you want to always get some kind of random results, you need to include some other factors such as the system time, cursor position, etc. For my project where I needed to use a random integer every 300ms after the game begins, I used the following routine: generate a random int every tick, but start getting it only when the player clicks the start game button (this way coincidence is virtually eliminated, so we are getting a pseudorandom sequence that looks like a random one to the player).

I think your problem might lie in the Get Accurate Real Time node. I looked this up and I have no idea what it does, ie. what time it actually gives you. I think you were hoping for the current OS time, but if you’re having this problem, I doubt that is what you are getting. actually made some blueprint functions for getting OS time which will probably give you the result you’re looking for. They can be found here:

(39) 's Extra Blueprint Nodes for You as a Plugin, No C++ Required! - Blueprint - Epic Developer Community Forums!

For anyone coming from C++ land like myself, this is what you want:

 int64 DateInSeconds = FDateTime::Now().ToUnixTimestamp();
 FRandomStream SRand = FRandomStream();
 SRand.Initialize(DateInSeconds);

Now, instead of using FMath::FRand() you would use SRand.FRand() or whatever random function you are using. I tested in a packaged build and it gives me a different sequence every time.