[已解决]从M个数中选N个随机数并且随机排列组合。
本帖最后由 cnergao 于 2014-3-7 15:24 编辑在下面的代码中这一句
$rStr &= $key[$Y]&","(后面加个逗号是为了获取数组后再分割的时候。逗号当分割符用。所以在这里专门加上逗号)
有时候运行提示如下错误
运行 AU3Check (1.54.23.0)开始目录:D:\AUTOIT_3.3.9.0-1
+>21:32:03 AU3Check 完成:0
>运行:(3.3.9.0):D:\AUTOIT_3.3.9.0-1\autoit3.exe "D:\AUTOIT_3.3.7.15.1\DIYau3\test.au3"
"D:\AUTOIT_3.3.7.15.1\DIYau3\test.au3" (15) : ==> ???????????????????.:
$rStr &= $key[$Y]&","
$rStr &= ^ ERROR->21:32:03 AutoIT3.exe 完成::1
+>21:32:04 ACNWrapper 完成..
首先声明。这段程序在不改动的情况下。为什么有时候运行正常。有时候提示下面的错误。不知道是什么原因。#include <Array.au3>
Local $Y, $keywords =
$file = FileOpen("d:\test.txt", 1)
If @error Then
MsgBox(0, "", "文件不存在")
EndIf
Local $rStr = ""
$num = Random(8, 15, 1)
;从rand中取Num个数
Dim $key = $keywords
For $j = 1 To $num
Local $Y = Random(1, UBound($key), 1)
$rStr &= $key[$Y]&","
_ArrayDelete($key,$Y)
Next
$rStr = StringTrimRight($rStr, 1)
$rand = StringSplit($rStr, ",")
;交替随机值
for $i=1 to $num
$r=Random(1,$num,1)
Local $Tmp = $rand[$i]
$rand[$i] = $rand[$r]
$rand[$r] = $Tmp
Next
_ArrayToClip($rand, 1, $num)
$Y = StringRegExpReplace(ClipGet(), "\s", ",")
FileWrite("d:\test.txt",$Y&@CRLF)
FileClose($file) 把代码改成下面的
在赋值的地方
$a[$j - 1] = $key[$Y]
还是出现这种情况。有时好。有时坏。这段要怎么写?#include <Array.au3>
Local $Y, $r, $keywords =
$file = FileOpen("d:\test.txt", 1)
If @error Then
MsgBox(0, "", "文件不存在")
EndIf
Dim $a
$num = Random(8, 15, 1)
ReDim $a[$num]
;从keywords中取Num个数
Dim $key = $keywords
For $j = 1 To $num
Local $Y = Random(1, UBound($key), 1)
$a[$j - 1] = $key[$Y]
_ArrayDelete($key, $Y)
Next
;交替随机值
For $i = 1 To UBound($a) * 3
_ArraySwap($a, $a)
Next
_ArrayToClip($a, 1, $num)
$Y = StringRegExpReplace(ClipGet(), "\s", ",")
FileWrite("d:\test.txt", $Y & @CRLF)
FileClose($file) 换了一个有提示的版本。以上的错误已经发现并解决。下面是解决方案和说明:
#include <Array.au3>
Local $Y, $r, $keywords =
$file = FileOpen("d:\test.txt", 1)
If @error Then
MsgBox(0, "", "文件不存在")
EndIf
Dim $a
For $conut=1 To 30
$num = Random(8, 15, 1)
ReDim $a[$num]
;从keywords中取Num个数
Dim $key = $keywords
For $j = 1 To $num
Local $Y = Random(1, UBound($key)-1, 1)
$a[$j-1]=$key[$Y]
_ArrayDelete($key, $Y)
Next
;交替随机值
For $i = 1 To UBound($a) * 3
_ArraySwap($a, $a)
Next
_ArrayToClip($a, 0, $num)
$Y = StringRegExpReplace(ClipGet(), "\s", ",")
FileWrite("d:\test.txt", $Y &"A值:"&$a&"$num值:"&$num&"$a维数:"&UBound($a)&@CRLF)
Next
FileClose($file)
出现赋值错语是因为:Array variable has incorrect number of subscripts or subscript dimension range exceeded.:。翻译过来的意思是:赋值的两个数组有一个下标超出了。所以有时候运行正常有时候出错。因为有时超出有时没有超出。百度翻译后自己理解的,意思如果不对大家指正。
解决办法是:由Local $Y = Random(1, UBound($key), 1)改成Local $Y = Random(1, UBound($key)-1, 1).
页:
[1]