Home   Help Search Login Register  

Author Topic: Random  (Read 1199 times)

0 Members and 1 Guest are viewing this topic.

HuNtEr-exz

  • Guest
Random
« on: 21 Jan 2003, 05:33:33 »
how wolud i get a script to select something using the random command? out of 3 things how would i get it to select one and only one  and how would i use it

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:Random
« Reply #1 on: 21 Jan 2003, 18:12:12 »
See if the following script code will work for you:

Code: [Select]
; _i the number of things from which you are choosing randomly between.

_i = 3

; Get a random number ranging from 0 to _i.

_n = random (_i)

; Loop until _n != _i since we never want a value of _i.

while "_n == _i" do {_n = random (_i)}

; Use the mod command to get a whole number.

_n = _n - (_n mod 1)

; Get the chosen thing and assign it to a variable.

_chosenThing = MyArray select _n

If it works the way I think it does, it should give you a random whole number ranging from 0 to (number of things - 1).  In your case, it would give a range of 0 to 2, which is what you need to select the elements of your array.
Ranger

HuNtEr-exz

  • Guest
Re:Random
« Reply #2 on: 22 Jan 2003, 09:19:32 »
so would this chose 1 between 6 sounds?

Code: [Select]
_myArray = [playSound "1"; playSound "2"; playSound "3"; playSound "4"; playSound "5"; playSound "6"]


; _i the number of things from which you are choosing randomly between.

_i = 6

; Get a random number ranging from 0 to _i.

_n = random (_i)

; Loop until _n != _i since we never want a value of _i.

while "_n == _i" do {_n = random (_i)}

; Use the mod command to get a whole number.

_n = _n - (_n mod 1)

; Get the chosen thing and assign it to a variable.

_chosenThing = MyArray select _n

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:Random
« Reply #3 on: 22 Jan 2003, 13:03:31 »
i donno bout rangers way but u can do it da easy way ;) :P

Code: [Select]
_myArray = [playSound "1"; playSound "2"; playSound "3"; playSound "4"; playSound "5"; playSound "6"]

_n = random (count _myArray)

_chosenThing = _myArray select _n

will work ;)

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

HuNtEr-exz

  • Guest
Re:Random
« Reply #4 on: 22 Jan 2003, 16:24:50 »
ok so if i wanted these sounds to play if units were in an area and stop when they die would i do this?
Code: [Select]
#loop
_myArray = [playSound "1"; playSound "2"; playSound "3"; playSound "4"; playSound "5"; playSound "6"]

_n = random (count _myArray)

_chosenThing = _myArray select _n
~20
goto #loop

#end
exit
and put in activation
this exec "scriptything.sqs"
and in deactivation
goto "end"

?? or am i missinfg somehting thx in advance

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:Random
« Reply #5 on: 22 Jan 2003, 19:32:22 »
i donno bout rangers way but u can do it da easy way ;) :P

_myArray = [playSound "1"; playSound "2"; playSound "3"; playSound "4"; playSound "5"; playSound "6"]

_n = random (count _myArray)

_chosenThing = _myArray select _n

I didn't think that would work because random gives you fractional values.  So, in this example, the count = 6, meaning that _n = random (6).  You would get values ranging from 0 to 6, including 0.0001, 2.7, 5.28937, and 6 (which is outside the range of the array).  Are you sure this words, LCD?
Ranger

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:Random
« Reply #6 on: 22 Jan 2003, 21:58:25 »
I apparently didn't read your original post well enough.  As far as I know, you cannot store the names of commands (playsound, in this case) in an array and have the array activate that command.  Your code would instead have to look like the following:

Code: [Select]
#loop
_myArray = ["1", "2", "3", "4", "5", "6"]

; _i is the number of things from which you are choosing randomly between.

_i = count _myArray

; Get a random number ranging from 0 to _i.

_n = random (_i)

; Loop until _n != _i since we never want a value of _i.

while "_n == _i" do {_n = random (_i)}

; Use the mod command to get a whole number.

_n = _n - (_n mod 1)

; Get the chosen thing and assign it to a variable.

playSound _myArray select _n
~20
goto #loop

Quote
and put in activation
this exec "scriptything.sqs"
and in deactivation
goto "end"

?? or am i missinfg somehting thx in advance

That is only partly correctly.  Putting goto "end" in the Deactivation field will not go to the #end in the script, as the trigger has no connection to the script except for the fact that it executes it.  You have to put an if-then statement inside the script to check if it needs to end.
« Last Edit: 22 Jan 2003, 22:01:50 by Ranger »
Ranger

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:Random
« Reply #7 on: 22 Jan 2003, 22:09:01 »
Quote
I didn't think that would work because random gives you fractional values.  So, in this example, the count = 6, meaning that _n = random (6).  You would get values ranging from 0 to 6, including 0.0001, 2.7, 5.28937, and 6 (which is outside the range of the array).  Are you sure this words, LCD?

work like a charm :P

just he need 2 change da play sound from his arraynd put it in his coomand later

if u dont belive me check it ;) also it wont give 5 ;) it wil give num between 0-6 not including 0 and 6 (or at least it never hapened 2 me ;) ;D and u dont have 2 make it (how da f00k u say num W/O numbers after da .?) thingy ;D OFP accepts ny num it wil just take it nd use it ;) if u dont belive me u can try ;D :D

and im editing 4 a year now so i gotta know ;) ;D

 :P :-* :P :-* :P :-*

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:Random
« Reply #8 on: 23 Jan 2003, 19:27:01 »
That would be a whole number.  8)

In any event, I believe you, man.  I've never tried using that method before, though, so I didn't know it would work.  I've been editing for about a year, too, but I haven't tried every editing possibility. :)

Thanks for the info, though.  That'll help simplify my code.
Ranger

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:Random
« Reply #9 on: 23 Jan 2003, 19:34:22 »
kool ;D

right

whole number

and how u call number wid nums after da . ?  ;)  :P

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta

Offline Ranger

  • Members
  • *
  • Hoo-ah!
Re:Random
« Reply #10 on: 23 Jan 2003, 22:19:29 »
and how u call number wid nums after da . ?  ;)  :P

I think that would be a non-integer, since integer is also a name for a whole number.

The numbers after the decimal point are known as the fractional-part.
Ranger

Offline LCD

  • Former Staff
  • ****
    • Everon Cartel
Re:Random
« Reply #11 on: 23 Jan 2003, 22:43:21 »
kool ;D

 ;D

tank u 4 eng lessong ;)

:cheers:

LCD OUT
"guess being the community has downsides .." - cheetah
Help Perfecting Da Next Best Thing - O-Team Beta