Random function work around
jhaas
Member Posts: 233
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
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
I guess my question is WHERE does the timer go?
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.