I am making an enemy obj for my new engine and when it spawns, i want it to be a 1/2 chance that it will either start moving left or right. I tried doing
dir = (rand(0,1)); //upon startup it will either move left or right
but I got an error cause it does not know what rand is (even thought i got the tutorial from the wiki). Can someone show me what to do? Thanks
have you included mod_rand? if you do something random in your game, your game won't be predictable anymore. This can become an issue if you want to create a self playing demo. I just tell in advance in case you want to create that later on.
what error?
it works now but i noticed the game returns the exact same value everytime i boot it up. even if i change rand(0,1) to like rand(0,10) then itll be 8 and if its rand(0,50) itll be 41 every time
its normal. In computer world is impossible do a real random function, all they are pseudo-random functions. So you need put a different seed on every different run.
A tweak to do it is the next:
rand_seed(time());
Isn't the rand_seed derived from the realtime clock and the date and some other hardware parameters?
I don't understand you.
rand_seed, set a seed, a seed is a number to start a gausian bell pseudo-randown function. Rand_seed set this seed to the internal method to calculate randown numbers. It can be used to sincronize to the same values of rand in sevaral differents machines.
You can pass as argument a different number in each run, using time() function, and it would seem that the application is using randown values, but if you coud execute the application twice in the same time value, you will obtain exactly the same values in the two instances of your application.
I don't know if there is an overload fuction rand_seed to do that you sais. You can do it manualty if you want do it, passing in the argument a obtained value using time and hardware values. ;)
http://www.cprogramming.com/tutorial/random.html
Quote from: DCelso on March 19, 2013, 06:43:09 AM
its normal. In computer world is impossible do a real random function, all they are pseudo-random functions. So you need put a different seed on every different run.
A tweak to do it is the next:
rand_seed(time());
all it returns is 1. there literally is NO way at all do a 1/2 chance thing?
put the rand_seed(time()); instruction at the beginning of the game, only once. before using the rand() function.
ah there we go, thanks so much!