mIRC Home    About    Download    Register    News    Help

Print Thread
Randomizing a list #234382 23/10/11 12:32 AM
Joined: Oct 2011
Posts: 4
L
lds Offline OP
Self-satisified door
OP Offline
Self-satisified door
L
Joined: Oct 2011
Posts: 4
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.

Re: Randomizing a list [Re: lds] #234384 23/10/11 01:27 AM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
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.


Invision Support
#Invision on irc.irchighway.net
Re: Randomizing a list [Re: Riamus2] #234385 23/10/11 02:07 AM
Joined: Oct 2011
Posts: 4
L
lds Offline OP
Self-satisified door
OP Offline
Self-satisified door
L
Joined: Oct 2011
Posts: 4
Thanks it works now, also didn't help that I had the wrong variable in the addtok line d'oh.

Re: Randomizing a list [Re: lds] #234389 23/10/11 08:25 AM
Joined: Jul 2007
Posts: 1,129
T
Tomao Offline
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
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)
} 


Re: Randomizing a list [Re: Tomao] #234390 23/10/11 08:42 AM
Joined: Feb 2006
Posts: 546
J
jaytea Offline
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
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.


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Re: Randomizing a list [Re: jaytea] #234391 23/10/11 09:06 AM
Joined: Jul 2007
Posts: 1,129
T
Tomao Offline
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
Ouch. That makes sense, and thank you, jaytea.

Re: Randomizing a list [Re: Tomao] #237905 13/06/12 07:49 PM
Joined: Jun 2012
Posts: 1
L
LunaLoire Offline
Mostly harmless
Offline
Mostly harmless
L
Joined: Jun 2012
Posts: 1
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?