Random function work around

jhaasjhaas Member Posts: 233
edited November -1 in Working with GS (Mac)
I've done some experimenting and if you REALLY need random numbers in your game this has worked for me....

First a quick recap - the random function generates the same pattern of "random" numbers once your game is running on the device. This can cause an issue when you want game levels or elements to be truly random. Since we can't rely on the code and the hardware to give you a random number alone - we must introduce a truly random ocurrance - and that is user input.

Here's what I'm doing - when my game first loads on my title screen the end user is presented with a "Start" button to begin the game. At that time I read the game's internal timer, taking just the mili-seconds and using that to "seed" the random function. Each time I need a new random number I find an appropriate time to grab user input to again read the timer mili-seconds and re-seed the random function.

Here's how...

First define an attribute of type real we'll call it "loop".

Define another attribute of type integer, we'll call it "number".

Now when a user presses a button or performs some type of interaction with your game - assign a value to "loop" by using the change attribute behavior where "loop" = (( game.Time *100)-(floor( game.Time )*100))/50

What that code does is take the game timer and strips of everything but the mili-seconds, makes it a whole number, (1 - 100) then divides it by 50.

We then user a "timer" behavior, set it to perform a "for" and use the "loop" variable as the number of seconds it will loop. Be sure and set the run to completion option on. Because we divided the "loop" value by 50 in the above step, the timer will run for up to 2 seconds. The behavior that is executing in the timer is the constrain attribute "number" to the random function.

Basically for "loop" seconds, we constantly assign random numbers to "number" using the random function. And since the number of times we assign the value is dependent on the value of the timer mili-seconds at the time the user performed some input, the result is a unique random number.

This may not work for everyone, but this should get the old creative juices flowing to perhaps modify this concept to work for you as well.

Happy coding!

Jeff :)

Comments

  • ktfrightktfright Member Posts: 964
    Just posting to bring this thread to light in case anyone needs a workaround for randoms.
  • design219design219 Member Posts: 2,273
    Wow, nice work!
  • ccbx4321ccbx4321 Member Posts: 43
    Any way you could upload this for tinkering? It's a bit silly that the random function doesn't truly randomize on it's own, but what are you gonna do?
  • wilefwilef Member Posts: 1
    I've been trying to get this to work, but can't for the life of me. You lose me when we do the timer. I have a timer that says FOR TimeLoop seconds (run to completion) Constrain Attribute game.number to random (1,2). The default value for game.number is 0, and when I run the game it never gets assigned a number at all. I've tried putting the timer in a couple different places (in the same button where the rest of the code is, and elsewhere) and it never gets assigned.

    I guess my question is WHERE does the timer go?
  • SwifishSwifish Member Posts: 4
    I needed to do this for angles and as a result of waiting for user input you can make it pseudo random.

    Just create a game attribute to store the "random" value you want.
    Create a Timer loop. i.e. every 0.1 generate a new random value between (x,y) and change attribute "game attribute" you just created.

    And then spawner or actor uses this value based on input or action.
    In my case it gives the appearance of randomness.

    Hope that helps someone.
  • dshoedshoe Member Posts: 62
    I could use some help on this. I have a game and I want to spawn actors on the x axis between 0 and 500 what would the code for setting the attribute of "loop", and would the timer be in the same actor as the behavior for changing the loop?
Sign In or Register to comment.