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 |
You would need to recurse rather than loop passing the modified array forward. On 16 April 2017 at 17:35, Toeb <[hidden email]> wrote: Hi! I've been attempting to shuffle values inside of a vector but I can't for _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
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); |
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: 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 swap(arr, index1, index2) = [for (i = [0:len(arr)-1]) // corrected i==index1 ? arr[index2] : i==index2 ? arr[index1] : arr[i]]; 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: 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.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, 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 <[hidden email]>: Ah thank you! I've been trying to mess around with it by creating a recursive _______________________________________________ OpenSCAD mailing list [hidden email] http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org |
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 |
Free forum by Nabble - Scala forum | Edit this page |