Jump to content

Bucket List Randomize


Mollymews
 Share

You are about to reply to a thread that has been inactive for 1515 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

a conversation here

https://community.secondlife.com/forums/topic/452728-mystery-prize-script-problem-with-timer/

led to a posit. How might we randomly choose a prize from a gumball machine of No-Copy items, so that no two No-Copy prizes of the same kind would be given twice in a row, while also knowing that the No-Copy items in the gumball machine's Contents are named like:

A Item, A Item 1, A Item 2, Another Item, Another Item 1, Lonely Item, Yet Another, Yet Another 1, Yet Another 2, Yet Another 3, etc

a way to do this is with a bucket list shuffle (randomizing) algorithm

this kind of algorithm is one of those obscure things which we hardly ever need or use in the normal course, until we do need it

list bucketRandomize(list items, integer taglen)
{  
    // taglen is the min. number of chars needed to tag (uniquely identify) 
    //   groups of items to go in separate buckets  
    // taglen must be a positive number in [0..n]    
    
    // decrement taglen to be a string position pointer
    if (--taglen < 0) taglen = 0;

    // prep items
    items = llListSort(items, 1, TRUE);
    integer len = llGetListLength(items);

    // find largest number of same tag. This is the number of buckets
    string item;
    integer maxc;
    integer maxp;
    integer c;
    integer p;
    integer i;
    for (i = 0; i < len; i++)
    {
        string tag = llGetSubString(llList2String(items, i), 0, taglen);
        if (tag == item) // increment (c)ount
            c++;
        else  // new tagged item
        {   
            if (c > maxc)
            {
                maxc = c; maxp = p;
            }
            item = tag; c = 1; p = i;
        }            
    }
    
    // move buckets to front
    items = llList2List(items, maxp, maxp + maxc - 1) + llDeleteSubList(items, maxp, maxp + maxc - 1);
 
    // fill buckets with remaining items
    c = 1; p = i = maxc;
    while (i < len)
    {
        while (p)
        {
            item = llList2String(items, i);
            // shuffle item into the bucket
            items = llListInsertList(llDeleteSubList(items, i, i), [item], p - (integer)llFrand((float)(c))); 
            p -= c; i++;     
        }
        c++; p = i;
    }  

    // shuffle buckets
    // note that this uses treble pipe |||. If ||| is in your data then use other chars as pipe 
    return llParseString2List(llDumpList2String(llListRandomize(items, c), "|||"), ["|||"], []); 
}


default
{
    state_entry()
    {
        list items = [
           "D", "D1", "D2", "D3", "D4", 
           "K",
           "E", "E1", "E2",
           "C", "C1", "C2", "C3",
           "B", "B1", "B2", "B3", "B4", "B5", 
           "H", "H1",
           "F", "F1", "F2", "F3", "F4",
           "A", "A1", "A2", "A3", "A4", "A5", "A6", "A7"
        ];
        
        llOwnerSay("*****");
        llOwnerSay("items: " + llDumpList2String(items, " "));
        
        items = bucketRandomize(items, 1);
        
        llOwnerSay("bucket: " + llDumpList2String(items, " ")); 
    }
}

 

  • Thanks 1
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1515 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...