mIRC Homepage
Posted By: lds Randomizing a list - 23/10/11 12:32 AM
So I'm trying to take a list of 12 objects, randomize it, and then display 3 at a time. I'm sorta a newb when it comes to scripting, so help would be appreciated. (player# are all just text strings edited for privacy reason)

Code:
on *:TEXT:!newlines:*: {
  set %newlines player1 player2 player3 player4 player5 player6 player7 player8 player9 player10 player11 player12
  set %newlinecounter 1
  while (%newlinecounter <= 12) {
    set %newlinerand $rand(1,$numtok(%newlines,32))
    echo -s $addtok(%newlinesdisp,$gettok(%newlines,%newlinecounter,32),32)
    echo -s $deltok(%newlines,%newlinerand,32)
    inc %newlinecounter 1
  }
  msg $chan $gettok(%newlinesdisp,1,32) - $gettok(%newlinesdisp,2,32) - $gettok(%newlinesdisp,3,32)
  msg $chan $gettok(%newlinesdisp,4,32) - $gettok(%newlinesdisp,5,32) - $gettok(%newlinesdisp,6,32)
  msg $chan $gettok(%newlinesdisp,7,32) - $gettok(%newlinesdisp,8,32) - $gettok(%newlinesdisp,9,32)
  msg $chan $gettok(%newlinesdisp,10,32) - $gettok(%newlinesdisp,11,32) - $gettok(%newlinesdisp,12,32)
}


I have no idea why it isn't working, so any help is appreciated.
Posted By: Riamus2 Re: Randomizing a list - 23/10/11 01:27 AM
You can't use echo to add or remove tokens. It will echo what you are doing, but won't actually change any variables. Replace echo -s with set. Or, if you don't need these variables anywhere other than in this script, then change all set to var to make them local variables (they don't remain set after they aren't needed anymore).

And, to make it easier on your message lines... before them, you can use:
Code:
tokenize 32 %newlinesdisp


And then, for your message lines, you can use:
Code:
msg $chan $1 - $2 - $3
msg $chan $4 - $5 - $6


etc.
Posted By: lds Re: Randomizing a list - 23/10/11 02:07 AM
Thanks it works now, also didn't help that I had the wrong variable in the addtok line d'oh.
Posted By: Tomao Re: Randomizing a list - 23/10/11 08:25 AM
Code:
on *:text:!newlines:#:{
  var %newlines = player1 player2 player3 player4 player5 player6 player7 player8 player9 player10 player11 player12
  tokenize 32 %newlines
  var %r = 1, %i
  while (%r <= $0) {
    %i = %i $($+($,$rand(1,$0)),2)
    inc %r
  }
  msg # $gettok(%i,1-3,32)
  msg # $gettok(%i,4-6,32)
  msg # $gettok(%i,7-9,32)
  msg # $gettok(%i,10-12,32)
} 

Posted By: jaytea Re: Randomizing a list - 23/10/11 08:42 AM
Originally Posted By: Tomao
Code:
on *:text:!newlines:#:{
  var %newlines = player1 player2 player3 player4 player5 player6 player7 player8 player9 player10 player11 player12
  tokenize 32 %newlines
  var %r = 1, %i
  while (%r <= $0) {
    %i = %i $($+($,$r(1,$0)),2)
    inc %r
  }
  msg # $gettok(%i,1-3,32)
  msg # $gettok(%i,4-6,32)
  msg # $gettok(%i,7-9,32)
  msg # $gettok(%i,10-12,32)
} 



nope. this just makes 12 random selections from %newlines, without replacement, that can and almost certainly will contain duplicates.

if he wanted to make the code more efficient, he could use:

Code:
  var %output
  while (%newlinecounter <= 12) {
    %output = $instok(%output, $gettok(%newlines, %newlinecounter, 32), $rand(1, %newlinecounter), 32)
    inc %newlinecounter
  }


then %output contains a scrambled version of %newlines.
Posted By: Tomao Re: Randomizing a list - 23/10/11 09:06 AM
Ouch. That makes sense, and thank you, jaytea.
Posted By: LunaLoire Re: Randomizing a list - 13/06/12 07:49 PM
how would the full script look lets say if i was trying to do 8 things and I wanted it to randomize all 8 things with no duplicates?
© mIRC Discussion Forums