Randomize an array in javascript and in Python

Randomizing arrays

To randomize an array in javascript there is this way:

a = [1,2,3,4,5]
a.sort(function() {return .5 - Math.random()});

In Python you can you shuffle from the random module

from random import shuffle

a = [1,2,3,4,5]
shuffle(a)

 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.