Download

Trying to get the same random float in 2 seperate functions

Hello everyone, I am working on an encryption mechanism that encrypts floats, I have a function that encrypts them using some math and a few “Random Float from Stream” nodes, I also have another function (This function also has the same “Random Float from Stream” nodes) that inputs an encrypted float and spits out the decrypted version. The problem is that to decrypt the float, I need to be getting the same values from the random floats. The reason I am using a stream is so that I can have predictable randoms, and while they are repeated/predictable, both of my functions are getting different values using the same stream. My question is, how can I get the same values from a random using a stream?

Edit: Before you say my math is wrong, the function properly decrypts if I replace all randoms in both functions with hard coded numbers.

Are you setting the ‘Initial Seed’ default value of the stream? It needs to be set in the blueprint, otherwise each blueprint’s stream is initialized to a different seed at startup…

Originally I had a default value for the initial seed set up in the stream, that did not work. Now I am calling “Set Random Stream Seed” on Event Begin Play to set a New Seed. I am calling both functions and printing the random float that they got, they are both different each time, they need to be the same regardless of what float is picked.

Edit: Here is the type of pattern I am looking for:

1st time:
Function 1 random: 82.96
Function 2 random: 82.96

2nd time:
Function 1 random: 17.18
Function 2 random: 17.18

The randoms I picked are arbitrary.

I think you’ll have to use multiple streams in order to get that behavior, from what I understand. The whole point of the stream is to give you a new repeatable/predictable number every time it’s accessed (without having to track your own seed values or iterators).

If you’re doing this over the network the client would have a stream and the server would have a stream for each player - all initialized using the same seed value. Every time the server calls an encrypt for a client, it uses the server-side player’s stream - then the client uses their local stream to decrypt it (thus both streams stay in ‘sync’ as to how many times they’ve been accessed).

You could probably also record how many times it’s been accessed, and build a macro to reset the seed and re-prime the stream (to count-1) before you re-pull, but it seems pretty hacky.

I now have 2 streams, one for encrypting, and another for decrypting. They are accessed the same number of times, and have the same ‘Initial Seed’ as well as the same seed that I am setting them to use on ‘Event Begin Play’. When a print out randoms using the streams, they return the same numbers, but do not when using them in my function. In the picture below, I can decrypt my variable to get the same number as before I encrypted it as long as I use the 9 & 2 as my numbers, but it does not work when I use the random instead of the 9.
a.png

float precision errors maybe? How far off are the numbers, and have you debugged to verify you’re actually getting different numbers from the stream? I made a simple test widget with 2 streams and a button for each, and it works just like described - each click updates each stream with the same number… so it should work.

They work if I just do a print string, but as soon as I hook up the random to a math operator like a float * float it messes them up the order even while just using 1 node for randoms. I know that it is not float precision error because I was also using a random boolean previously. The setup above is exactly what I have, I am not sure why it is not working.

Edit: Regarding the float precision error, the numbers are completely different.

Oh duh - actually, your encryption function is calling the stream twice. It calls it once to set the float (by ref), then the return value is a completely separate pull. You need to create a local float, call the random stream one and set it to that local float… then use that float to set (by ref) and return. That way it’ll only pull from the stream once.

I tried that earlier, but I will try again and report back.

Well, it turns out that I was decrypting twice per frame, and only encrypting once, so my streams were messed up. I fixed that and started using the local variables but it still is not working. I am actually just going to drop this idea altogether, but thanks for helping anyways.