mircscripting.info Forum Index mircscripting.info
#mIRCscripting Forum
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Random Distribution

 
Post new topic   Reply to topic    mircscripting.info Forum Index -> Snippets
View previous topic :: View next topic  
Author Message
Ook
Major


Joined: 01 May 2004
Posts: 82

PostPosted: Wed Nov 20, 2019 6:48 pm    Post subject: Random Distribution Reply with quote

Here are a couple of aliases to randomly distribute a list of words between a number of lists.

$random_distribution(3,john nick bob liz fred susan pantz nobby bill)
returns a set of lists like: "nick bob nobby" "pantz susan bill" "john fred liz"

$random_distribution_table(3,john nick bob liz fred susan pantz nobby bill)
returns a table name like: rand_dist_601790392

Use the /test_random_distribution alias to see it work.

Code:

; randomly distribute a string of words into x lists
; $1 = number of lists, $2- = list of words
; returns each list in "" on a single line
alias random_distribution {
  ; check for valid args
  if (($0 < 2) || ($1 !isnum 2-)) return ERROR: Invalid args
  var %list = 1, %in = $2-, %items = $numtok(%in,32), %table = $+(rand_dist_,$ticks)
  ; check number of items is a multiple of the number of lists.
  ; uncomment this line if you only want an even distribution
  ;if ($calc(%items % $1)) return ERROR: Number of items ( %items ) is not a multiple of $1
  hmake %table 100
  while ($numtok(%in,32) > 0) {
    var %item = $r(1,$v1), %this = $gettok(%in,%item,32), %in = $deltok(%in,%item,32)
    hadd %table $+(out_,%list) $hget(%table,$+(out_,%list)) %this
    inc %list
    if (%list > $1) var %list = 1
  }
  var %res, %lists = $1
  while (%lists > 0) {
    var %res = %res $qt($hget(%table,$+(out_,%lists)))
    dec %lists
  }
  hfree %table
  return %res
}
; randomly distribute a string of words into x lists
; $1 = number of lists, $2- = list of words
; returns the name of the hashtable containing the results, where each item in the table is a list
alias random_distribution_table {
  ; check for valid args
  if (($0 < 2) || ($1 !isnum 2-)) return ERROR: Invalid args
  var %list = 1, %in = $2-, %items = $numtok(%in,32), %table = $+(rand_dist_,$ticks)
  ; check number of items is a multiple of the number of lists.
  ; uncomment this line if you only want an even distribution
  ;if ($calc(%items % $1)) return ERROR: Number of items ( %items ) is not a multiple of $1
  hmake %table 100
  while ($numtok(%in,32) > 0) {
    var %item = $r(1,$v1), %this = $gettok(%in,%item,32), %in = $deltok(%in,%item,32)
    hadd %table $+(out_,%list) $hget(%table,$+(out_,%list)) %this
    inc %list
    if (%list > $1) var %list = 1
  }
  return %table
}
alias test_random_distribution {
  var %t = john nick bob liz fred susan pantz nobby bill
  var %lists = 3
  echo combined lists: $random_distribution(%lists,%t)
  var %table = $random_distribution_table(%lists,%t)
  echo lists table: %table
  var %c = %lists
  while (%c > 0) {
    echo list %c :: $qt($hget(%table,$+(out_,%c)))
    dec %c
  }
  hfree %table
  echo combined uneven lists: $random_distribution(%lists,%t goblin)
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    mircscripting.info Forum Index -> Snippets All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group