discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Shuffling vectors

T
Toeb
Sun, Apr 16, 2017 4:35 PM

Hi! I've been attempting to shuffle values inside of a vector but I can't for
the life of me figure out a solution of how to do it.

I've managed to create a function to swap two elements within a vector,
however, I haven't managed to use this to do a Fisher-Yates style shuffle as
it seems variables inside of a for loop do not retain their new value. This
means I can't loop through doing

Array = swap (Array, 0, rands(i, len(Array) - 1), 1, seed);

Or something of that sort. I'm very new to the language used in OpenSCAD so
I don't have the experience to work with its intricacies. Although maybe
someone here does and can help me while also providing information for
someone that may be struggling with this in the future.

Thank you in advance

--
View this message in context: http://forum.openscad.org/Shuffling-vectors-tp21242.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Hi! I've been attempting to shuffle values inside of a vector but I can't for the life of me figure out a solution of how to do it. I've managed to create a function to swap two elements within a vector, however, I haven't managed to use this to do a Fisher-Yates style shuffle as it seems variables inside of a for loop do not retain their new value. This means I can't loop through doing Array = swap (Array, 0, rands(i, len(Array) - 1), 1, seed); Or something of that sort. I'm very new to the language used in OpenSCAD so I don't have the experience to work with its intricacies. Although maybe someone here does and can help me while also providing information for someone that may be struggling with this in the future. Thank you in advance -- View this message in context: http://forum.openscad.org/Shuffling-vectors-tp21242.html Sent from the OpenSCAD mailing list archive at Nabble.com.
NH
nop head
Sun, Apr 16, 2017 5:00 PM

You would need to recurse rather than loop passing the modified array
forward.

On 16 April 2017 at 17:35, Toeb toby.a.bennnett@gmail.com wrote:

Hi! I've been attempting to shuffle values inside of a vector but I can't
for
the life of me figure out a solution of how to do it.

I've managed to create a function to swap two elements within a vector,
however, I haven't managed to use this to do a Fisher-Yates style shuffle
as
it seems variables inside of a for loop do not retain their new value. This
means I can't loop through doing

Array = swap (Array, 0, rands(i, len(Array) - 1), 1, seed);

Or something of that sort. I'm very new to the language used in OpenSCAD so
I don't have the experience to work with its intricacies. Although maybe
someone here does and can help me while also providing information for
someone that may be struggling with this in the future.

Thank you in advance

--
View this message in context: http://forum.openscad.org/
Shuffling-vectors-tp21242.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

You would need to recurse rather than loop passing the modified array forward. On 16 April 2017 at 17:35, Toeb <toby.a.bennnett@gmail.com> wrote: > Hi! I've been attempting to shuffle values inside of a vector but I can't > for > the life of me figure out a solution of how to do it. > > I've managed to create a function to swap two elements within a vector, > however, I haven't managed to use this to do a Fisher-Yates style shuffle > as > it seems variables inside of a for loop do not retain their new value. This > means I can't loop through doing > > Array = swap (Array, 0, rands(i, len(Array) - 1), 1, seed); > > Or something of that sort. I'm very new to the language used in OpenSCAD so > I don't have the experience to work with its intricacies. Although maybe > someone here does and can help me while also providing information for > someone that may be struggling with this in the future. > > Thank you in advance > > > > -- > View this message in context: http://forum.openscad.org/ > Shuffling-vectors-tp21242.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
T
Toeb
Sun, Apr 16, 2017 6:07 PM

Ah thank you! I've been trying to mess around with it by creating a recursive
function.  I think I've worked out how to do the recursion but I can't
figure out how to implement it. So here is my swap function.

function swap(arr, index1, index2) = [for (i = arr) i==index1 ? arr[index2]
: i==index2 ? arr[index1] : arr[i]];

Now here is the kind of thing I'm trying to work out with the shuffle.

function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0, num-1,
1, seed)) : shuffle(arr, num-1, seed);

I think for it to work I would need to have it so if the "num<=2" condition
is true it would just call swap for the last time but if the condition was
false it would call swap /and /call the shuffle function again. However I'm
not sure how I would do that. So like

function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0, num-1,
1, seed)) : swap(arr, num-1, rands(0, num-1, 1, seed)) then shuffle(arr,
num-1, seed);

--
View this message in context: http://forum.openscad.org/Shuffling-vectors-tp21242p21244.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Ah thank you! I've been trying to mess around with it by creating a recursive function. I think I've worked out how to do the recursion but I can't figure out how to implement it. So here is my swap function. function swap(arr, index1, index2) = [for (i = arr) i==index1 ? arr[index2] : i==index2 ? arr[index1] : arr[i]]; Now here is the kind of thing I'm trying to work out with the shuffle. function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0, num-1, 1, seed)) : shuffle(arr, num-1, seed); I think for it to work I would need to have it so if the "num<=2" condition is true it would just call swap for the last time but if the condition was false it would call swap /and /call the shuffle function again. However I'm not sure how I would do that. So like function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0, num-1, 1, seed)) : swap(arr, num-1, rands(0, num-1, 1, seed)) then shuffle(arr, num-1, seed); -- View this message in context: http://forum.openscad.org/Shuffling-vectors-tp21242p21244.html Sent from the OpenSCAD mailing list archive at Nabble.com.
RP
Ronaldo Persiano
Sun, Apr 16, 2017 7:03 PM

There is some mistakes in your codes. First, the function swap() is
comparing array entry values with indices. I think the following is what
you intended:

function swap(arr, index1, index2) =
[for (i = [0:len(arr)-1]) // corrected
i==index1 ? arr[index2]
: i==index2 ? arr[index1] : arr[i]];

In the function shuffle, rands() is misused. The function rands() always
returns a vector of floats even if just one is needed. So, to make sense it
would be written as:

function shuffle(arr, num, seed) =
num<=2 ?
swap(arr, num-1, ceil(rands(0, num-1,1, seed)[0]) )
: shuffle(arr, num-1, seed);

But, this will not work as intended either because all operations are done
over the same starting array arr. What we need is to shuffle a swapped
array instead:

function rend(num,seed) = ceil(rands(0, num-1,1, seed)[0]);

function shuffle(arr, num, seed) =
num<2 ?
swap(arr, num-1, rend(num,seed) ):
shuffle(swap(arr,num,rend(num,seed)), num-1, seed);

That is not ideal though. Function rend() has a very low probability of
returning 0 and low probability to return num-1 and should be better
defined.

Finally note that each time rands() is called with the same parameters and
a seed, it will return exactly the same vector.

2017-04-16 15:07 GMT-03:00 Toeb toby.a.bennnett@gmail.com:

Ah thank you! I've been trying to mess around with it by creating a
recursive
function.  I think I've worked out how to do the recursion but I can't
figure out how to implement it. So here is my swap function.

function swap(arr, index1, index2) = [for (i = arr) i==index1 ? arr[index2]
: i==index2 ? arr[index1] : arr[i]];

Now here is the kind of thing I'm trying to work out with the shuffle.

function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0,
num-1,
1, seed)) : shuffle(arr, num-1, seed);

I think for it to work I would need to have it so if the "num<=2" condition
is true it would just call swap for the last time but if the condition was
false it would call swap /and /call the shuffle function again. However I'm
not sure how I would do that. So like

function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0,
num-1,
1, seed)) : swap(arr, num-1, rands(0, num-1, 1, seed)) then shuffle(arr,
num-1, seed);

--
View this message in context: http://forum.openscad.org/Shuffling-vectors-
tp21242p21244.html
Sent from the OpenSCAD mailing list archive at Nabble.com.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

There is some mistakes in your codes. First, the function swap() is comparing array entry values with indices. I think the following is what you intended: function swap(arr, index1, index2) = [for (i = [0:len(arr)-1]) // corrected i==index1 ? arr[index2] : i==index2 ? arr[index1] : arr[i]]; In the function shuffle, rands() is misused. The function rands() always returns a vector of floats even if just one is needed. So, to make sense it would be written as: function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, ceil(rands(0, num-1,1, seed)[0]) ) : shuffle(arr, num-1, seed); But, this will not work as intended either because all operations are done over the same starting array arr. What we need is to shuffle a swapped array instead: function rend(num,seed) = ceil(rands(0, num-1,1, seed)[0]); function shuffle(arr, num, seed) = num<2 ? swap(arr, num-1, rend(num,seed) ): shuffle(swap(arr,num,rend(num,seed)), num-1, seed); That is not ideal though. Function rend() has a very low probability of returning 0 and low probability to return num-1 and should be better defined. Finally note that each time rands() is called with the same parameters and a seed, it will return exactly the same vector. 2017-04-16 15:07 GMT-03:00 Toeb <toby.a.bennnett@gmail.com>: > Ah thank you! I've been trying to mess around with it by creating a > recursive > function. I think I've worked out how to do the recursion but I can't > figure out how to implement it. So here is my swap function. > > function swap(arr, index1, index2) = [for (i = arr) i==index1 ? arr[index2] > : i==index2 ? arr[index1] : arr[i]]; > > Now here is the kind of thing I'm trying to work out with the shuffle. > > function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0, > num-1, > 1, seed)) : shuffle(arr, num-1, seed); > > I think for it to work I would need to have it so if the "num<=2" condition > is true it would just call swap for the last time but if the condition was > false it would call swap /and /call the shuffle function again. However I'm > not sure how I would do that. So like > > function shuffle(arr, num, seed) = num<=2 ? swap(arr, num-1, rands(0, > num-1, > 1, seed)) : swap(arr, num-1, rands(0, num-1, 1, seed)) then shuffle(arr, > num-1, seed); > > > > > -- > View this message in context: http://forum.openscad.org/Shuffling-vectors- > tp21242p21244.html > Sent from the OpenSCAD mailing list archive at Nabble.com. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
T
Toeb
Sun, Apr 16, 2017 8:53 PM

Oh wow, I can't thank you enough. I don't think I could have figured this out
on my own. You're completely right about the swap function. I had been using
a vector that was just each incremental value so of course it looked like it
was working the way I wanted but wasn't really.

It never occurred to that I could use swap as and input to my shuffle
function as the solution. Hopefully I will get better at using a language
that functions the way this one does. Thank you again

--
View this message in context: http://forum.openscad.org/Shuffling-vectors-tp21242p21248.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Oh wow, I can't thank you enough. I don't think I could have figured this out on my own. You're completely right about the swap function. I had been using a vector that was just each incremental value so of course it looked like it was working the way I wanted but wasn't really. It never occurred to that I could use swap as and input to my shuffle function as the solution. Hopefully I will get better at using a language that functions the way this one does. Thank you again -- View this message in context: http://forum.openscad.org/Shuffling-vectors-tp21242p21248.html Sent from the OpenSCAD mailing list archive at Nabble.com.