1. #1

    Shuffle vector contents

    I want to use a vector and shuffle it contents each time the track is reset. Like shuffling a deck of cards. I have written the code for this in AS3, using arrays, but I could not find a "push" or "splice" event for vectors. Is there a way to do this?
    I figure I could use generic filters, but I would have to test everything as I go along. To "splice" from one vector and "push" to another vector seems a lot easier.

    I can post my AS3 code if that would help.
    Thanks.
    Share this post

  2. #2
    By push and slice I assume you are trying to insert a value into a vector for resorting?

    Ex.)

    Vector Cells #: 0 1 2 3 4 5 6
    Vector Values: 3 4 7 2 4 9 8

    Move data value 7 from cell 2 to cell 5

    Vector Cells #: 0 1 2 3 4 5 6
    Vector Values: 3 4 2 4 9 7 8

    To do these kinds of vector operations you're going to have to use Generic Filters in an intra-interval impulse loop, i.e. an impulse chain that completes a "loop" within a single interval. They function similarly to a "for" loop in AS, C++, Java, etc.

    Connect a single impulse (possibly from a Impulse Trigger or some other provider) to a Generic Filter tied to a Variable Data Source. This VDS will serve as your loop counter, incrementing with each successful iteration of your loop. Send the Generic Filter's True Event off to perform your vector operations. Then at the end of your vector operations connect it back to a Set Value Event that will increase your loop counter and send the Set Value Event back into your Generic Filter. This will create a loop and allow you to do a bunch of calculations on your vectors without spawning in a bunch of toolcards.

    Intra-Interval Impulse Loop
    Code:
    Single Impulse --> Generic Filter --> --> --> *perform your vector operations*
                          ^        ^                              |
                          |        |                              |
                          |        |                              v
                          VDS <-- Increment the Loop Coutner <-- <--


    Beware though, this intra-interval impulse chain will break after ~250 impulses, so the more pieces you add to the "Vector Operations" side, the less number of times you can iterate This is just a precaution by Redlynx to prevent infinite loops, but ~250 impulses is still very useful with vector calculations.

    I think I also know how to implement your vector operations, but I'm confused as to the exact function you are looking for. By "push", do you want to open a space inside a vector, and "push" all the values one cell over? Or do you want to "push" a new cell onto the end of the vector? Same thing for splice, are you attempting to combine two vectors? Or are you attempting to insert a value within a vector?

    Hope this is of some help to you
    - PneumaticBog484
    Share this post

  3. #3
    Hey PneumaticBog484, thanks for the reply...
    Yes, "push" means add to the array, and "splice" means delete from the array. The final result would be an array, or vector, with the values shuffled differently every time it resets the track.
    It may be easier to understand looking at my code.

    Here is my AS3 code:

    var originalArray:Array=new Array(); /// create and initialize a new array
    var shuffledArray:Array=new Array(); /// create and initialize a new array
    var t:int=0; /// var for holding array index
    var lengthOfArray:int=10; // length of array

    function setUpArray(){
    originalArray = []; // clears out originalArray
    for (var i=1; i<=lengthOfArray; i++) {
    originalArray.push(i); // a for loop to build originalArray = 1,2,3,4,5,6,7,8,9,10
    }

    // shuffle list
    shuffledArray = []; // clears out shuffledArray

    while (originalArray.length>0) { // loop while originalArray is not empty
    t = int(Math.random()*originalArray.length); // pick a random number between 0 and originalArray length
    shuffledArray.push(originalArray[t]); // add this number to the shuffledArray
    originalArray.splice(t, 1); // delete that number from originalArray
    }

    trace("shuffledArray="+shuffledArray); // outputs values like a javascript alert.
    }
    setUpArray();

    OUTPUT: shuffledArray=9,4,10,7,2,3,5,8,6,1

    Every time you run this script the shuffledArray has the values at a different index.
    I think I can do it with a bunch of generic filters, but it would be nice to do it like the script above.
    Thanks for your help.
    Share this post

  4. #4
    Okay, I see what you're trying to do. I'll try to whip up a toolcard image later, but for now I'll explain what you need to do in words.

    I think ActionScript allocates memory storage for your Array as the program runs, which is why you don't have to specify "This Array will have 10 values" up there in the var statement. Unfortunately Trials doesn't let you perform memory allocations during runtime ( ), so you will have to resort to a Jerry-Rigged approach. This approach will make both of these operations possible. Also, before I explain the approach, note that in Trials the very first Vector Index is 0, and then continues up by one. I know some languages begin at 1, and I don't know which way ActionScript is.

    Quasi-Resizable Vector
    First, when creating your Vectors/Arrays in Trials, always set the Length property to the highest value that you think you will need. For your example there, I would set it to 11 just to be safe. Then, place a Variable Data Source next to each Vector. This Data Source will act as the "Current Size" of your Vector. All cells with Indices less than the "Current Size" will be considered values that exist in your Vector. For example, if the "Current Size" of that 11-Length Vector is 7, then cells 0 through 6 are considered valid entries in your Vector. All cells with Indices greater than or equal to the "Current Size" will be considered garbage values, since they technically don't exist. I normally create a function to set my garbage values to -1.0 so that I can easily identify them as garbage when testing.

    So long as your Vectors use the above format, then these next two operations will work.

    "Push" Operation
    This type of operation can be performed without the intra-interval loop described in my last post. Whenever you wish to add a value to the end of the Vector, use a Set Vector Event to set the cell with Index number "Current Size" to your intended value. Then increment your "Current Size" Variable Data Source by one. So long as you keep your "Current Size" less than the actual Length of the Vector, you can add multiple values to the end of said Vector.

    "Splice" Operation
    This type of operation will require the intra-interval loop described in my last post. Before sending the impulse to your loop, use a Set Value Event to change the value of your Loop Counter (the VDS connected to the Generic Filter) to the cell Index which you want to delete. Also, use another Set Value Event to decrement the "Current Size" VDS by one. Now send an impulse into your loop starting with the Generic Filter. On the Vector Operations side, create a Set Vector Event which will copy the value of the cell one to the right of Loop Counter, into the cell at the Loop Counter. You can do this by adding one to the Loop Counter with a Two-Input Operator, and then using a Get Vector to get the value. Now connect your Set Vector Event back to the Set Value Event which increments your Loop Counter. This setup will now iterate through each cell, starting with the cell you want removed, and replace it with a copy of the cell on its right. This way the value you wanted deleted will be covered up by a copy, but all the other values will be maintained because they are simply copied one cell over. All cells before the deleted cell will remain the same.

    Ex.) "Splice" cell 5

    Actual Size: 10
    Current Size: 8
    Vector Indices: 0 1 2 3 4 5 6 7 8 9
    Vector Values: G W Q T U I O _ _


    1st Iteration 'I' was copied over 'U'
    Loop Counter: 5
    Vector Values: G W Q T I I O _ _

    2nd Iteration 'O' was copied over 'I'
    Loop Counter: 6
    Vector Value : G W Q T I O O _ _

    3rd Iteration Optional step where 'O' was given a garbage value
    Loop Counter: 7
    Vector Value: G W Q T I O _ _ _


    Actual Size: 10
    Current Size: 7
    Vector Indices: 0 1 2 3 4 5 6 7 8 9
    Vector Values: G W Q T I O _ _ _

    I'll try to make a quick demonstration in the Editor if I get time this weekend.

    I hope this helps you.
    - PneumaticBog484
    Share this post

  5. #5
    Hey, yeah it helped a lot! I did get it to work. Thanks again! I actually built mine before I saw your reply. Mine is different, being in that my only goal was to have to final vector populated with random values and I didn't really need the splicing. I would however like to see your version, if you have time, and I will post mine here too. Thanks again!
    Share this post