Skip to content
Advertisement

Create an array with random values

How can I create an array with 40 elements, with random values from 0 to 39 ? Like

JavaScript

I tried using solutions from here:

http://freewebdesigntutorials.com/javaScriptTutorials/jsArrayObject/randomizeArrayElements.htm

but the array I get is very little randomized. It generates a lot of blocks of successive numbers…

Advertisement

Answer

Here’s a solution that shuffles a list of unique numbers (no repeats, ever).

JavaScript

If you want to allow repeated values (which is not what the OP wanted) then look elsewhere. 🙂

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement