mardi 18 février 2020

JS Array Shuffle not working on variable definition

After searching for an array shuffler, I've implemented the second post on this question: How to randomize (shuffle) a JavaScript array?

However, for some reason, it only works for me when not assigning it to a variable. After assigning it, the randomization is gone. I'm not sure what's happening:

https://js.do/code/406652

<script>

                
        /// import seeded random number generator code
        // https://github.com/davidbau/seedrandom/blob/released/seedrandom.min.js
        !function(a,b){function c(c,j,k){var n=[];j=1==j?{entropy:!0}:j||{};var s=g(f(j.entropy?[c,i(a)]:null==c?h():c,3),n),t=new d(n),u=function(){for(var a=t.g(m),b=p,c=0;q>a;)a=(a+c)*l,b*=l,c=t.g(1);for(;a>=r;)a/=2,b/=2,c>>>=1;return(a+c)/b};return u.int32=function(){return 0|t.g(4)},u.quick=function(){return t.g(4)/4294967296},u["double"]=u,g(i(t.S),a),(j.pass||k||function(a,c,d,f){return f&&(f.S&&e(f,t),a.state=function(){return e(t,{})}),d?(b[o]=a,c):a})(u,s,"global"in j?j.global:this==b,j.state)}function d(a){var b,c=a.length,d=this,e=0,f=d.i=d.j=0,g=d.S=[];for(c||(a=[c++]);l>e;)g[e]=e++;for(e=0;l>e;e++)g[e]=g[f=s&f+a[e%c]+(b=g[e])],g[f]=b;(d.g=function(a){for(var b,c=0,e=d.i,f=d.j,g=d.S;a--;)b=g[e=s&e+1],c=c*l+g[s&(g[e]=g[f=s&f+b])+(g[f]=b)];return d.i=e,d.j=f,c})(l)}function e(a,b){return b.i=a.i,b.j=a.j,b.S=a.S.slice(),b}function f(a,b){var c,d=[],e=typeof a;if(b&&"object"==e)for(c in a)try{d.push(f(a[c],b-1))}catch(g){}return d.length?d:"string"==e?a:a+"\0"}function g(a,b){for(var c,d=a+"",e=0;e<d.length;)b[s&e]=s&(c^=19*b[s&e])+d.charCodeAt(e++);return i(b)}function h(){try{if(j)return i(j.randomBytes(l));var b=new Uint8Array(l);return(k.crypto||k.msCrypto).getRandomValues(b),i(b)}catch(c){var d=k.navigator,e=d&&d.plugins;return[+new Date,k,e,k.screen,i(a)]}}function i(a){return String.fromCharCode.apply(0,a)}var j,k=this,l=256,m=6,n=52,o="random",p=b.pow(l,m),q=b.pow(2,n),r=2*q,s=l-1;if(b["seed"+o]=c,g(b.random(),a),"object"==typeof module&&module.exports){module.exports=c;try{j=require("crypto")}catch(t){}}else"function"==typeof define&&define.amd&&define(function(){return c})}([],Math);


        //shuffling function
        function shuffleArray(array) {
                for (var i = array.length - 1; i > 0; i--) {
                        var j = Math.floor(Math.random() * (i + 1));
                        var temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                }
        return(array)
        }
        
        Math.seedrandom(10)

        // Create attribute variable
        
        var attributes = ["a", "b", "c" , "d", "e", "f", "g", "h", "i", "j", "k", "l"]
        
        // Shuffle indexes
        var idx = shuffleArray([0,1,2,3,4,5]);

        document.write(shuffleArray(attributes))
    document.write(shuffleArray(attributes))
    
   function generate_profile() {
                var ret = {};
        ret["value"] = shuffleArray(attributes)
                return(ret["value"])
        }
        
        var des_a = generate_profile();
        var des_b = generate_profile();

        a_list = ["a1","a2","a3","a4","a5", "a6"]; 
        b_list = ["b1","b2","b3","b4","b5", "b6"]; 
    


        for(i=0;i<6;i++){
                document.write(des_a[idx[i]]);
                document.write(des_b[idx[i]]);
        }

</script>



Aucun commentaire:

Enregistrer un commentaire