0

## PowerShell 亂數問題

``````for (\$i = 0 ; \$i -lt 4 ; \$i++){

\$array[\$i] = @(get-random -input 1, 2, 3, 4, 5, 6 ,7 ,8, 9 ,0)

for (\$j = 0 ; \$j -lt \$i ; ){

if (\$array[\$j] -eq \$array[\$i]){
\$array[\$i] = @(get-random -input 1, 2, 3, 4, 5, 6 ,7 ,8, 9 ,0)
\$j =0

} else {
\$j++

}

}

}

\$array
``````

### 2 個回答

2

iT邦大師 1 級 ‧ 2019-10-11 12:13:59

narumi iT邦新手 5 級 ‧ 2019-10-11 12:19:29 檢舉

narumi iT邦新手 5 級 ‧ 2019-10-11 13:21:18 檢舉

3
froce
iT邦大師 1 級 ‧ 2019-10-11 13:25:31
``````\$shuffle = (0..9)

function arrayShuffle([int[]]\$s){
\$count = \$s.count
\$r = @()
for (\$i -eq 0; \$i -lt \$count; \$i++){
\$_r = Get-Random -input \$s
\$s = \$s| Where-Object { \$_ –ne \$_r }
\$r = \$r + [array]\$_r
}
return \$r
}

write-host((arrayShuffle \$shuffle)[0..3])
``````

``````\$shuffle = (0..9)

function arrayShuffle([int]\$t,[int[]]\$s){
if (\$t -lt \$s.count){
\$count = \$t
}else{
\$count = \$s.count
}

\$r = @()
for (\$i -eq 0; \$i -lt \$count; \$i++){
\$_r = Get-Random -input \$s
\$s = \$s| Where-Object { \$_ –ne \$_r }
\$r = \$r + [array]\$_r
}
return \$r
}

write-host((arrayShuffle 4 \$shuffle))
write-host((arrayShuffle 9 \$shuffle))
write-host((arrayShuffle 11 \$shuffle))
``````

narumi iT邦新手 5 級 ‧ 2019-10-11 13:40:30 檢舉