"roll the dice" variable?

Started by MisterN, March 18, 2013, 03:31:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

MisterN

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
werg

handsource-dyko

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.

SplinterGU

Download Lastest BennuGD Release: http://www.bennugd.org/node/2

MisterN

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
werg

DCelso

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());

Monstruos Diabólicos

"A PAck of classic GAMEs For BennuGD" en desarrollo
http://code.google.com/p/apagame4be/

handsource-dyko

Isn't the rand_seed derived from the realtime clock and the date and some other hardware parameters?

DCelso

#6
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
Monstruos Diabólicos

"A PAck of classic GAMEs For BennuGD" en desarrollo
http://code.google.com/p/apagame4be/

MisterN

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?
werg

gecko

put the rand_seed(time()); instruction at the beginning of the game, only once. before using the rand() function.
Torres Baldi Studio
http://torresbaldi.com

MisterN

ah there we go, thanks so much!
werg