Bennu Game Development

English Forums => Helpdesk => Topic started by: MisterN on March 18, 2013, 03:31:53 PM

Title: "roll the dice" variable?
Post by: MisterN on March 18, 2013, 03:31:53 PM
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
Title: Re:"roll the dice" variable?
Post by: handsource-dyko on March 18, 2013, 04:49:37 PM
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.
Title: Re:"roll the dice" variable?
Post by: SplinterGU on March 18, 2013, 04:51:17 PM
what error?
Title: Re:"roll the dice" variable?
Post by: MisterN on March 19, 2013, 01:49:29 AM
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
Title: Re:"roll the dice" variable?
Post by: 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());

Title: Re:"roll the dice" variable?
Post by: handsource-dyko on March 19, 2013, 08:12:03 AM
Isn't the rand_seed derived from the realtime clock and the date and some other hardware parameters?
Title: Re:"roll the dice" variable?
Post by: DCelso on March 19, 2013, 08:59:36 AM
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
Title: Re:"roll the dice" variable?
Post by: MisterN on March 20, 2013, 03:02:45 PM
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?
Title: Re:"roll the dice" variable?
Post by: gecko on March 20, 2013, 03:15:07 PM
put the rand_seed(time()); instruction at the beginning of the game, only once. before using the rand() function.
Title: Re:"roll the dice" variable?
Post by: MisterN on March 20, 2013, 03:47:29 PM
ah there we go, thanks so much!