OneCompiler

[JavaScript] How to create an array of random numbers?

I want to create an array of length 30 with random numbers whose values should be less than 50, how can we do that in Javascript?

1 Answer

4 years ago by

You can use the below code to create an array of desired length and with value limit.

Array.from({length: desiredLength}, () => Math.floor(Math.random() * valueLimit))

Check and Run the working sample here : https://onecompiler.com/javascript/3xmz6dc79

4 years ago by Meera