OFPEC Forum

Editors Depot - Mission Editing and Scripting => OFP - Editing/Scripting General => Topic started by: The-Architect on 28 Feb 2007, 04:34:50

Title: Random
Post by: The-Architect on 28 Feb 2007, 04:34:50
How do I make this randomly go to 4 possibilities and what's the reasoning behind how.

Code: [Select]
#Begin
_random = random 2
?(_random> 0) && (_random <= 1) : goto "radio1"
?(_random> 1) && (_random <= 2) : goto "radio2"

Cheers.
Title: Re: Random
Post by: bedges on 28 Feb 2007, 08:37:13
Code: [Select]
;choose 1 of 4 possibilities

_poss = random 4
_poss = _poss - (_poss mod 1)
_jump = format ["markername_%1", _poss]
goto _jump

#markername_0

#markername_1
etc...

_poss will be between 0 and 4, which is actually 5 possibilities in the highly unlikely even that random 4 generates exactly 4.
_poss mod 1 is the remainder following the decimal point. if you subtract that you get a round number.
_jump is created from a standard marker name, and is sequential from 0 to 4 because you stick _poss on the end, whatever it is.
even though random 4 could potentially give 5 possibilities, its hitting 4 is extremely unlikely. nevertheless, you should put in a marker for it anyway, just in case. i usually just go to marker 0.
Title: Re: Random
Post by: Mandoble on 28 Feb 2007, 09:15:32
To avoid that problem you may use random numberofpossibilities - 0.01
Title: Re: Random
Post by: macguba on 28 Feb 2007, 10:29:28
But then you hit the problem of 0 at the other end .....  :D

You could use

_poss = _poss*0.99
Title: Re: Random
Post by: THobson on 01 Mar 2007, 14:07:04
It is a while since I have done any of this, but something the following might do what you need:

Code: [Select]
_labels = ["radio1",radio2","radio3","radio4"]
#again
_rnd = random count _labels
_rnd = _rnd - (_rnd % 1)
if (_rnd >= count _labels) then {goto"again"}
goto (_labels select _rnd)
exit

#radio1
.
.
#radio2
.
.
#radio3
etc

You might need to replace:
goto (_labels select _rnd)

with
_label = _labels select _rnd
goto _label

The advantage of this approach is that you can easily add new destinations simply by adding the label name to the array.


Or some such
Title: Re: Random
Post by: THobson on 02 Mar 2007, 08:54:49
Apologies for the duplicate post, but this additional thought might be missed if I only edit yesterday's post.

Looking at the labels you are using radio1 etc. I wonder if what you are trying to do is to generate random radio messages. If that is the case then the following would be neater:

Code: [Select]
_messages = [["msg1name",msg1duration],["msg2name",msg2duration],["msg3name",msg3duration]]
;"msg1name" is the name of the first sound file and should be in quotes as shown
; msg1duration is the duration of the sound file in seconds
; eg _messages = [["sound1", 20],["sound2",15]...]

#again
_rnd = random count _messages
_rnd = _rnd - (_rnd % 1)
if (_rnd >= count _messages) then {goto"again"}
; I believe the above line to be un-necessary - but if it is necessary then it prevents an error
_msg = _messages select _rnd
playsound (_msg select 0)
~ (_msg select 1)

That way if you want to add a new message just put an extra array element in _messages.

Also if you want some messages to have a higher probability of occurring than others you can simply repeat their entry in _messages, eg:
_messages = [["sound1",20],["sound2",15],["sound2",15],["sound3",17]]
will result in sound2 having a 50% chance of being played and sound1 and sound 3 each having a 25% chance.