M
M
Mikhail2018-07-31 00:31:33
JavaScript
Mikhail, 2018-07-31 00:31:33

How to make a random array of 40 unique elements?

At first I thought that it was possible to shove a randomized array into a set, but I did not understand how to get a specific number of elements at the output.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-07-31
@Kowka_VN

At first I thought that it was possible to shove a randomized array into a set ...

It should be the other way around - first set, then array:
const set = new Set;
while (set.size < 40) {
  set.add(Math.random() * 100 | 0);
}
const arr = [...set];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question