---
# System prepended metadata

title: js randon array
tags: [JS]

---

# js randon array
###### tags: `JS`

使用Fisher–Yates shuffle演算法隨機排序
function
```
const shuffle = (array) => {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]]; 
  }
  return array;
}
```


run
```
let customNumbers =Math.floor(Math.random()*43);
shuffle([...Array(49).keys()].map(x => x + 1))
.slice(customNumbers,customNumbers+6);
```

```
let customNumbers =Math.floor(Math.random()*33);
let power = shuffle([...Array(39).keys()]
    .map(x => x + 1)).slice(customNumbers,customNumbers+6);
power.push(Math.floor(Math.random()*8)+1);
power;
```

```

```