Home   Help Search Login Register  

Author Topic: Randoms in scripts  (Read 1071 times)

0 Members and 1 Guest are viewing this topic.

Offline OFPfreak

  • Members
  • *
  • Who is da operation flashpoint freak now, freak?!
    • OFP NFS mod
Randoms in scripts
« on: 12 Aug 2005, 06:38:18 »
How do I create a part in a script wich randomly selects something e.g.

Code: [Select]
random 0.1
tank1 setdammage 1
random 0.1
tank2 setdammage 1
random 0.1
tank3 setdammage 1
random 0.1
tank4 setdammage 1
random 0.1
tank5 setdammage 1
random 0.1
tank6 setdammage 1
random 0.1
tank7 setdammage 1
random 0.1
tank8 setdammage 1
random 0.1
tank9 setdammage 1
random 0.1
tank10 setdammage 1

exit
ofp nfs REBORN! All new nitro, neon, customized cars, off-road, on-road, rally, bikes, racer models and more!
ofp nfs's homepage

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Randoms in scripts
« Reply #1 on: 12 Aug 2005, 09:08:04 »
I am not sure what you mean.

The script you show will set some random numbers and quite seperately will destroy all the tanks.

If you want to destroy a tank at random try something like:


_tanks = [tank1,tank2,tank3,tank4,tank5,tank6,tank7,tank8,tank9,tank10]
_index = random (count _tanks)
_index = _index - (_index % 1)

(_tanks select _index) setDammage 1
« Last Edit: 12 Aug 2005, 09:08:42 by THobson »

Offline 456820

  • Contributing Member
  • **
Re:Randoms in scripts
« Reply #2 on: 12 Aug 2005, 09:09:05 »
i think i get this

_rnd = random 10
_rnd = _rnd - (_rnd mod 1)

?_rnd==1:goto "option1"
?_rnd==2:goto "option2"
?_rnd==3:goto "option3"

copy that bit until you get to 10 since in the example there are 10 different things

then have this

#option1
put your first option here
EXIT

option2
next option
EXIT

option3
and so on till you get to 10
EXIT

that should do it there maybe an easier way
but ope that helps

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Randoms in scripts
« Reply #3 on: 12 Aug 2005, 09:29:45 »
456820's solution is also good, but not quite right.

_rnd = random 10
_rnd = _rnd - (_rnd mod 1)

will create random intergers in the range 0 to 9 not 1 to 10

so the first test should be:
?_rnd==0:goto "option0"

and you should continue until you get to 9.
« Last Edit: 12 Aug 2005, 09:30:03 by THobson »

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Randoms in scripts
« Reply #4 on: 12 Aug 2005, 21:35:19 »
what happens on the oh-so-rare event that the random number comes out at exactly 10, and therefore mod leaves it there? lol

Offline THobson

  • OFPEC Patron
  • Former Staff
  • ****
Re:Randoms in scripts
« Reply #5 on: 13 Aug 2005, 08:58:49 »
Triggerhappy, indeed.  in all my scripts I do something like this:

_tanks = [tank1,tank2,tank3,tank4,tank5,tank6,tank7,tank8,tank9,tank10]
#another
_index = random (count _tanks)
_index = _index - (_index % 1)
if (_index >= count _tanks) then {goto"another"}

(_tanks select _index) setDammage 1


I don't know exactly how the random function has been implemented, does random n  generate a number  >=0 and <= n; or is the range >0 and < n.  I don't know so I normally use the belt and braces approach above but I have noticed that others don't.

Offline Triggerhappy

  • Contributing Member
  • **
  • Llama, it's what's for dinner.
Re:Randoms in scripts
« Reply #6 on: 13 Aug 2005, 15:33:42 »
well, even if it does have the ability to happen, the possibility is so slim that i doubt people bother, or they just never thought of it. i usually just add a check to see if it is the highest number (10 in this case) and if so,subtract 1 so it is what it should be