Question - Is there any way to randomly pull a variable from an array?

Screwing around with the Bazaar.mac and I've done very little in regards to programming using macro language but I've got a fair amount of experience with programming in general. I was wondering, if I declare an array (which hopefully I did right) is there any way to randomly choose a value from that array?

For example, say I wanted my determinant percentage that calculates my items price to be chosen from 3 different values:

Array
INI:
    /declare pricePercentage[3]  float     outer
    /varset  pricePercentage[1]  0.92
    /varset  pricePercentage[2]  0.95
    /varset  pricePercentage[3]  0.97
Would be my arrays declaration (I think), how would I randomly choose a value to throw in to the equation calculating the items price, something like a misc.random?

Or is there a better way to do this?

Thanks! :)
 
Code:
/declare randomInt int outer 0
could be created as an outer variable.

and according to mq2 wiki for math TLO.
Code:
  /echo ${Math.Rand[500]}
Echoes a random number between 1 and 500

So then we can then
Code:
/varset randomInt ${Math.Rand[3].Int}
/echo ${pricePercentage[${randomInt}]} is the value stored at ${randomInt}

Hope this helps.
 
Code:
/declare randomInt int outer 0
could be created as an outer variable.

and according to mq2 wiki for math TLO.
Code:
  /echo ${Math.Rand[500]}
Echoes a random number between 1 and 500

So then we can then
Code:
/varset randomInt ${Math.Rand[3].Int}
/echo ${pricePercentage[${randomInt}]} is the value stored at ${randomInt}

Hope this helps.
It looks like Math.Rand uses 0 which ends up being null. :( I'll toy around with it, but anyone know any way to fix this? Pic for reference, works great otherwise!

Edit:
Whipped up a simple fix:
Null fix
INI:
            /varset percentagePicker ${Math.Rand[3]}
                    /if (${percentagePicker}<1) {
                        /varcalc percentagePicker ${percentagePicker}+1
                        /echo Null avoided!
                    }
Threw this before the varcalc on the percentage. Essentially just sets the variable, checks if it's 0 and if it is sets it to 1 then does the varcalc and now it's working like a charm! Thanks again! :)

Edit 2:
Realized Math.Rand can take a min argument so I kept the exception catcher in there just in case but instead used:
Math.Rand
INI:
/varset percentagePicker ${Math.Rand[1,5]}
so 1 is the minimum and 5 is the maximum (increased array size to 5)

Hopefully last edit! Thanks again! :)
 

Attachments

  • bazerror.png
    bazerror.png
    18.2 KB · Views: 13
Last edited:
Question - Is there any way to randomly pull a variable from an array?

Users who are viewing this thread

Back
Top
Cart