找回密码
 加入
搜索
查看: 2222|回复: 2

[AU3基础] [已解决]从M个数中选N个随机数并且随机排列组合。

[复制链接]
发表于 2014-3-6 21:35:42 | 显示全部楼层 |阅读模式
本帖最后由 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 完成[/code]: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 完成:[/code]:1
+>21:32:04 ACNWrapper 完成..

首先声明。这段程序在不改动的情况下。为什么有时候运行正常。有时候提示下面的错误。不知道是什么原因。
#include <Array.au3>
Local $Y, $keywords[20] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
$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)
 楼主| 发表于 2014-3-6 23:18:40 | 显示全部楼层
把代码改成下面的
在赋值的地方
$a[$j - 1] = $key[$Y]
还是出现这种情况。有时好。有时坏。这段要怎么写?
#include <Array.au3>
Local $Y, $r, $keywords[20] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
$file = FileOpen("d:\test.txt", 1)
If @error Then
        MsgBox(0, "", "文件不存在")
EndIf


Dim $a[1]

$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[Random(0, UBound($a) - 1, 1)], $a[Random(0, UBound($a) - 1, 1)])
Next
_ArrayToClip($a, 1, $num)
$Y = StringRegExpReplace(ClipGet(), "\s", ",")
FileWrite("d:\test.txt", $Y & @CRLF)

FileClose($file)
 楼主| 发表于 2014-3-7 09:32:03 | 显示全部楼层
换了一个有提示的版本。以上的错误已经发现并解决。下面是解决方案和说明:
#include <Array.au3>
Local $Y, $r, $keywords[20] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
$file = FileOpen("d:\test.txt", 1)
If @error Then
        MsgBox(0, "", "文件不存在")
EndIf


Dim $a[1]
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[Random(0, UBound($a) - 1, 1)], $a[Random(0, UBound($a) - 1, 1)])
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).
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-10 07:29 , Processed in 0.077516 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表