Customapi RandomNumber.php on loop returns the same number

Hi, i got this code which is returning me the same number

$(user) Rolled $(eval $(1) != null ? $(1):1)d$(2): $(eval total=0;value=0;message=;for(i=0;i<$(eval $(1) != null ? $(1):1);i++){ value = $(customapi$(2)); message += “[” + value + "] "; total += value; }; message += "Total: " + total)

But if i try this, it works

$(user) Rolled $(eval $(1) != null ? $(1):1)d$(2): $(eval total=0;value=0;message=;for(i=0;i<$(eval $(1) != null ? $(1):1);i++){ value = Math.floor((Math.random() * $(2)) + 1); message += “[” + value + "] "; total += value; }; message += "Total: " + total)

and even this works

$(user) Rolled $(customapi$(2)) $(customapi$(2)) $(customapi$(2)) $(customapi$(2))

Am i missing a totally basic javascript concept here? or customApi calls doesnt work on eval loops?


In Nightbot commands, inner variables always evaluate before outer variables, and variables only ever evaluate once. This means that you cannot use a “for” loop to repeatedly call a custom API. Whatever result you get from randomnumber.php will be used over and over in your $(eval) code.

The randomnumber API was written before the $(eval) variable was introduced. Generating random numbers can now be done quite easily with Math.random() in JS. The following code performs the same task but within one $(eval) and includes input checking (making sure the inputs are positive integers).

$(user) Rolled $(eval n=decodeURIComponent(`$(querystring)`).split(` `).map(r=>parseInt(r)).filter(r=>!isNaN(r)&&r>0);if(n.length==2){a=n[0];b=n[1];m=``;t=0;v=0;for(i=0;i<a;i++){v=Math.ceil(Math.random()*b);m+=`[${v}] `;t+=v;}`${m}Total: ${t}`.slice(0,400);}else{`Enter 2 valid integers to roll!`;})

thanks for the reply. I was planning on using this for RPG dice roll. Do you think math.random() is enough randomness for this? considering it cannot be seedable.

Users may have different standards for what looks random. I suggest trying the command a few times and seeing if it gives a distribution of numbers that you can comfortably say is random enough.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.