Random number generator without dupes in Javascript?

后端 未结 6 1790
一生所求
一生所求 2020-11-28 14:53

I need help with writing some code that will create a random number from an array of 12 numbers and print it 9 times without dupes. This has been tough for me to accomplish.

6条回答
  •  悲哀的现实
    2020-11-28 15:32

    Try this once:

    //Here o is the array;
    var testArr = [6, 7, 12, 15, 17, 20, 21];
        shuffle = function(o){ //v1.0
                            for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
                            return o;
                    };
    shuffle(testArr);
    

提交回复
热议问题