Is Anyone Else Having Trouble with the Random Function?

geoascenciogeoascencio Member, PRO Posts: 74

Hello, GS community!

I'm encountering an issue with the Random function when using the Change Attribute behavior to set a real number. Instead of producing a random result, it always lands on the same number. For example, if I use random(1,3), it consistently returns 2 every time. For one actor, random(1,10) always lands on 8, and for another actor, it always lands on 4.

Has anyone else experienced this, or am I missing something? I’d appreciate any insights or advice you can share. Thanks in advance for your help!

Comments

  • adent42adent42 Key Master, Head Chef, Executive Chef, Member, PRO Posts: 3,157
    edited October 18

    @geoascencio GameSalad uses a PRNG (psuedo random number generator).

    Short version:

    • The number sequence appears random, but is the same every time.
    • You can change the sequence by setting the randomSeed attribute.
    • To feel more "truely" random, set the seed to the the current epochTime before grabbing random numbers.

    Long version:

    Computers can't do truly random numbers, they just used an algorithm that generates sequences of numbers that are random enough. These are called pseudo-random number generators.

    These algorithms usually have a "seed" that help determine how the algorithm starts which produces a different sequence of random numbers.

    For games this can be desirable because you can use the same seed and give two players the same "random" game experience OR you could recreate the same random experience on server and client and see if there is a different for cheating.

    If you look in a lot of languages, you'll note that the random number function often takes an argument, which is this seed. Or they'll create a random number object that takes the seed as an argument and has a function to get the "next" random number.

  • geoascenciogeoascencio Member, PRO Posts: 74

    @adent42 thanks for taking the time to reply to my post.

    I think something might be wrong with my game file. I’m not the best at explaining issues, but I ran a little experiment to test my theory. I started a fresh project and set up the following behaviors in an actor:

    Then, in my UltraKick game file, I created a new scene and used the same behaviors in a new actor. I played the scene for 8 seconds and reset it several times. In the UltraKick FC game file, the sequence of numbers I got was 12122313. Every time I reset the scene, the numbers appeared in the same order 12122313. I restarted my computer and restated Game Salad and still got 12122313.

    When I tried the same setup in the new game file, I got different results every time I reset the scene. This seems weird, and I'm concerned because UltraKick uses the random function for things like the match commentator (so he doesn’t repeat the same dialogue every time) or determining the AI's reaction time in single-player mode.

    I first noticed this issue when the commentator was saying the same victory line every time, which confused me since this hadn’t happened before.

    The good news is that when I tested this on devices using the GameSalad Viewer app, the issue didn't occur, and the random function worked correctly, giving me a different sequence of numbers each time I reset the scene. I’m a bit relieved that this only happens when I preview the game on the desktop version of GameSalad, but I’m worried this could be a sign of file corruption.

    Should I be concerned?

  • adent42adent42 Key Master, Head Chef, Executive Chef, Member, PRO Posts: 3,157

    Look at the value of the Game attribute "Random Seed" for both of those projects. -1 means that we handle the work of making everything as random as possible. Any number other than -1 will be used as a seed and cause the issue you are seeing.

  • geoascenciogeoascencio Member, PRO Posts: 74

    @adent42 Thank you for taking the time to reply to my post. The Random Seed attribute was at 0, and changing it to -1 fixed the issue! Thanks so much!

Sign In or Register to comment.