How to shuffle one slider packs values based on the indexes of another?
-
@LeeC Put the values in an array and generate a random array index.
-
Hey @d-healey,
The solution you posted above is great but I have been pulling my hair out for the last 24hrs trying to modify it to actually shuffle the positions within the array (for specified indexes only), as opposed to just generating a random number (as you have in your snippet).
I came across a code snippet online that would work perfectly for what I'm trying to achieve but it makes use of a Javascript 'Splice' method that we don't have in HISE.
Can you think of a smart/alternative achieve the same thing?The code is below and you can test it out at using the following url link text
Thanks again
function shuffle(array) { var currentIndex = array.length , temporaryValue , randomIndex ; // While there remain elements to shuffle... while (0 !== currentIndex) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } function shuffle2(array,aux){ var i; var keeper = []; for(i=0;i<aux.length;i++){ keeper[i]=array[aux[i]]; } for(i=aux.length-1;i>=0;i--){//Its important to do this from the last to the first array.splice(aux[i],1);//this will remove the index element shifting the rest of elemnts } shuffle(array); //now we put back the elements we took out. for(i=0;i<aux.length;i++){ array.splice(aux[i],0,keeper[i]);//this will remove the index element shifting the rest of elemnts } return array; } a = [1,2,3,4,5] shuffle2(a, [1, 3]) document.body.innerHTML = JSON.stringify(a);
-
You want to shuffle the positions in the array that has been set by sliderpack1?
-
@d-healey Yes exactly - Shuffle around the values in sliderpack1 but only for the position indexes provided by sliderpack2....
I probably didn't make that clear in my initial post but you gave me enough for me to attempt to implement it myself. Yet here I am again asking for guidance
The functionality that I'm trying to achieve is the same as this http://jsfiddle.net/n2kKd/
-
Looking at the JSFiddle would you get the same result by generating a random number between 1 and 5 for each element, ignoring values that are already in the array?
Like this for example:
HiseSnippet 736.3ocsU0taSCCE0tqFQCTDShG.y+ZkFUov1.oAhw5GnJXaUzwDRnoIOGmEqkXG43rQ0zdm4M.tNoasCpJaUh7qb+3Xex8dt2Lzn4hrLsAgqcv3TAB+XxnwJaTmHlTgFzEgeBYWVlUXnkt1YbJKKSDfv3U9nyAtVUTwyOe+NrXlhKl5BgNTK4hOKSj1odGt8mjww8YAhCjIyj85aOfqUczw5bfOqP7QoL9YrSE6wboUgfvOnWfzpMirLqHCgqtiNX7nH8Epx7OTlIOIV3LZiFAGTo6953.GicdQchjwACu96NCAmxvoUgUJqBOirqLPdi+oUimVDfNEwr0CbkEQu1+a54OO5gmgdUKo2pjQbiL0NMhiaOhLPAMpPFzBlkVk4hpbDlzQCYnrsRXmI5a.iaPzXSe+0na362bq5d08fFQlkdNyPYFC8czueTg6K.1InM.eshEpSsQz2R2nYcuKq6QgGW9JH6cY1nVFlJ.N9FsWito6PcIHCoMdtCMb9Vnll0P0ro6JZklmEAFa4ck6d.dloiEsRMR3DrFGAgrZ1bqvbE2J0JpVsm1J1W0no2kd07txi9mgBCmaLWMvniiEl4F1oIMKBXCUdxIByZvWabt3lDglys69O3t084ksjYRTqFnj18SESrWr7EMomBu80AcYVlSxLwGjWpvXkN5f6JNGlEKEP0HcEYmY0ovz3eot.MtNHOlYusv2MsOI.TOtkByohTYR63Y2FbOlF7uyCq2U5tJYnzxilOeqLG9BUs+27cxtk5jdggBtcJYqR5+skcQx8fJeQmakpSggSi7GHLYu7jQvpVt.XhRIhgKhfq3TTk19NaWkYjPETX7K3YRv1Na7jfsuNHJgwM5i4kSJtsWOrvCvIUwR7ZveS.aZaTwzCfi32xGk.KUOlyckhW.be9Xd4Rf4UKAl0WBLarDX1bIv75k.yaVHF2+y9PtUmTNl.NF1qXMBF2Sw.UVghD8ann7KLC
-
@d-healey you're right maybe the JSFiddle example wasn't the best...
If you look back at my original snippet you'll notice that the values in sliderpack1 are just repositioned to different places in the array.
HiseSnippet 1374.3ocsW0saaaCElJMpswatXsX.6VtdyjWZcTRZZWWVwricRmwZRMp6J1vVQGiDUDQjHMjnRiaQ.5E6AXOB698RzGo8FrcnnjLiicZiwBuvVmC4geemyg+bX+DgGMMUjfrV54iFRQVep8fQbYXmPBii50EYcC6cIoRZBVqZqQCIooTejk0U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UZGKx3F0TfagLHyUu.guYPl2ObtQuHTbiBz065xQdI6hsfFn96GafZqeokjdrZlsK14iRIGQ6w6mPgszpayYo6JhAbHIfYVKXBrsF35E.+Dx9znbbutc92lvhdoAr+0AEvdKnXO35Bc7AmGwtH.XFMe+uYBvaJ.3KreFMfB2t4QmIJW6Cfx5ltQGCT9ygEnn1m+ZluLb7ZqsaERYGDJMK27h.58LAsqYJ6nJPcmDztmAzu8BA5FlftkAnn8J.cQ6Mb++1UuuAp3+vLKF89Ks36CL.8csMcUuYGemJnmsbYnncgeVDQd5J4UOeonC3JoSUxrprXdJSNxbu+kR48erz8l18YRuvoy2ElBeU2lcIy2hGKU2d6f.pmbLYWzdme9x+kQnmIxfZnO.p6MgAqdrUk4AucziBLgyoQpxFfSHUllK6pjUQlATtetv+BshNWUIaUz4pkchhIdIhW4ouXV8brqmqA3DO+UoKAmXBx3pp.rsca5hhgWI9JOOUn3t.2mtMqMG1r9bXy8lCa1XNr49ygMOXNr4aNWaTOPuclTDq2l.J5ucdwMVVayIvpr7Ujn+CyuTlL
Your snippet got me over the bridge by referencing sliderpack2 for the indexes to change. But you generated a random number between 0-50 as opposed to repositioning sliderpack1's values across the given indexes.
-
@LeeC Oh I think I get it now. You want to use sliderPack1's existing values as the min/max rather than generating new values between 0 and 50, correct?
-
@d-healey yes that's it.
The values in sliderpack1 never change they just reposition and they occur with the same frequency.For example:
Main Sliderpack1 = [ 5, 8, 7, 2, 4, 3, 3, 6]
Reference Sliderpack2 = [ 1, 0, 0, 0, 1, 1, 1, 1 ]
So looking at the data above, we would only expect the items highlighted in bold to be shuffled around after clicking the button as these are the only positions/indexes where the reference sliderpack2 has a value==1. So the potential outcomes might be:
[ 4, 8, 7, 2, 5, 3, 6, 3]
or
[ 3, 8, 7, 2, 6, 4, 3, 5]
Note how the number 3 always occurs with the same frequency.
-
This seems to work. It's a little messy so you might need to tidy it a bit.
HiseSnippet 1576.3ocsX0saaaCElJMpswaoXsX.6V1dyjQacbRaZWmW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vwQKVcb0xU0lsxqtXE0O1ZnI8GvIA5d5sXVi3psQT2CecVzoZiUTm4HBf7I4mk+QNuWWKXygsIww6B9dvjIWsMcx7aITzWxCpBeHXtJmTAO4R86O00rFKlJm5x5NXjmmhA7rg6RkE0WEBBeJe7dE7O6dEbakIzDWbDTv2fyTuLgxOqNHP1fIzKwLVVAvqx6j3N1NIJqgPLnog48c13gxoNPQOcWZ5yoPd08ezRv2opIa0Bd2oC7paW30yzi697QiqspYrgDWPjWpD4YOEx8ueswdLieV6wm8YsQnebiNZMgVgrABH3jPkJlNt60gd.zhpoup476PS2WIRPdUFU1g7twEHVczn9Ya95lGOZv661bHobMzFMgdpS5wdG0sAZyAZsFJx3N8KiahbHy0u.guyfLeH4RitMTbKK5lcb4HOmuc6mCp+1QNn17WZpnGosreOygCnTxAzM3cguOQU5NUYoaJz8aPjfZdy3BruA34s.+Bxtz3bbuoe9+6BKZGGX+68rvdG3hLv2YMwGbdD6h.faz7C+pK.uyBvW4+JZenWQ3JPmIJ23ifxibci1Nn7WIVTz6wOjEoFTVasVyAT1dCTtWk5h.5icAsiaJ6fQfVeRP6bJP+1KDnK6B5pNfh1xB5r9KW++aW8INnh+C2rX7GtxhuO0Az22x0UCO636TA8zWEDtPpHJKlnF+Vp5qlaW.9bzXWGTekOdJScr6d+qjqt9oR2a62koBGLc9NyT3q9KYWw709CALu+Z86SCUkjcV+0+4q9a8idkHCt1vdvc8jLn5wW2SnHSFRAlv4zXcKCvIjZUyGWu36y8n7n7A+K7XWbQ8XO6hKVrHZHITJdan4Cy5epgalOCvId9u3xbvIlvXbYG.90qUGMjEwdaXnNT7Pf6SWmktD57nKgNO9RnyxWBcdxkPmmdIz4aNWcz+3SsxThglsIvDcWKu4FOu03DnJKuhD8e.FfQiB
-
That's it @d-healey
Wow, I'm blown away by the speed with which you work all this out!True legend!