Autor Tema: "roll the dice" variable?  (Leído 3000 veces)

MisterN

  • Hero Member
  • *****
  • Mensajes: 650
  • Karma: 4
"roll the dice" variable?
« en: Marzo 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
Código: [Seleccionar]
dir = (rand(0,1)); //upon startup it will either move left or rightbut 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

  • Hero Member
  • *****
  • Mensajes: 568
  • Karma: 32
  • I'm the eye in the sky.
    • dyko designs
Re:"roll the dice" variable?
« Respuesta #1 en: Marzo 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.

SplinterGU

  • Hero Member
  • *****
  • Mensajes: 12929
  • Karma: 378
Re:"roll the dice" variable?
« Respuesta #2 en: Marzo 18, 2013, 04:51:17 pm »
what error?
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

MisterN

  • Hero Member
  • *****
  • Mensajes: 650
  • Karma: 4
Re:"roll the dice" variable?
« Respuesta #3 en: Marzo 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
werg

DCelso

  • Hero Member
  • *****
  • Mensajes: 3708
  • Karma: 181
Re:"roll the dice" variable?
« Respuesta #4 en: Marzo 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());

Monstruos Diabólicos

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

handsource-dyko

  • Hero Member
  • *****
  • Mensajes: 568
  • Karma: 32
  • I'm the eye in the sky.
    • dyko designs
Re:"roll the dice" variable?
« Respuesta #5 en: Marzo 19, 2013, 08:12:03 am »
Isn't the rand_seed derived from the realtime clock and the date and some other hardware parameters?

DCelso

  • Hero Member
  • *****
  • Mensajes: 3708
  • Karma: 181
Re:"roll the dice" variable?
« Respuesta #6 en: Marzo 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
« última modificación: Marzo 19, 2013, 09:05:34 am por DCelso »
Monstruos Diabólicos

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

MisterN

  • Hero Member
  • *****
  • Mensajes: 650
  • Karma: 4
Re:"roll the dice" variable?
« Respuesta #7 en: Marzo 20, 2013, 03:02:45 pm »
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

  • Hero Member
  • *****
  • Mensajes: 811
  • Karma: 47
    • Torres Baldi Studio
Re:"roll the dice" variable?
« Respuesta #8 en: Marzo 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.

MisterN

  • Hero Member
  • *****
  • Mensajes: 650
  • Karma: 4
Re:"roll the dice" variable?
« Respuesta #9 en: Marzo 20, 2013, 03:47:29 pm »
ah there we go, thanks so much!
werg