随机不重复数组的提问

遇到了问题?请先进行搜索(中文和英文),然后在此提问

Moderators: arcticir, tmplinshi

lychee219
Posts: 1
Joined: 03 Jun 2020, 21:38

随机不重复数组的提问

Post by lychee219 » 03 Jun 2020, 21:51

我想从1-23里取出16个组成一个随机不重复的数组,但是下面的代码没效果,哪位大佬帮我改改

Code: Select all

F3::
{   
    A := []
    lencount=0
    While(lencount<15)
    {
        res := % rnd(1,23)
        If %res% not In %A% 
        {
           A[A_Index]:= %res%
           lencount++
        }
    }
    str := ""
    Loop,% A.Length()
    {
        str .= A[A_Index] . ","
    }
    MsgBox,% str


    rnd(mn,mx) 
    {
        Random, num, %mn%,%mx%
        return num
    }
}

User avatar
Yakshongas
Posts: 587
Joined: 21 Jan 2020, 08:41
Facebook: Yakshongas

Re: 随机不重复数组的提问

Post by Yakshongas » 04 Jun 2020, 07:25

尝试使用此代码,它会生成16个对话框,其中包含从1到23的16个数字,而不会以随机顺序重复。

Code: Select all

F3::
    Numbers :="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23"
    Sort, Numbers, Random D,
    NumbersArray := StrSplit(Numbers, ",")
    
    Loop, 16
    {
        MsgBox, % NumbersArray[A_Index]
    }
Return
Please mark your topics as solved if you don't need any further help. ✅

Need a little more help? Discord : Yakshongas#9893 🕹

garry
Posts: 2412
Joined: 22 Dec 2013, 12:50

Re: 随机不重复数组的提问

Post by garry » 06 Jun 2020, 13:05

@Yakshongas thank you

Code: Select all

;-------- saved at 星期六 六月 2020-06-06  17:56 UTC --------------
;- 随机不重复数组的提问  / user=lychee219 / solution = Yakshongas 
;- https://www.autohotkey.com/boards/viewtopic.php?f=27&t=76862
;- Numbers :="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23"

#warn
#Singleinstance,force
numbers:=""
e:=""

$F3::
    loop,23
      numbers .= a_index . ","
    ;msgbox,%numbers%
    Sort, Numbers, Random D,
    NumbersArray := StrSplit(Numbers, ",")
    i=0
    Loop,16
       {
       i++
       aa:=NumbersArray[A_Index]
       e .= aa . ","
       ;msgbox, 262208,RANDOM-LOOP (16)  I=%i%,%aa%,2  ;- show for 2 seconds
       }
sort,e,N D,
msgbox, 262208,LOOP-END,%e%
e=
aa=
numbersarray=
numbers=
return
esc::exitapp


User avatar
Yakshongas
Posts: 587
Joined: 21 Jan 2020, 08:41
Facebook: Yakshongas

Re: 随机不重复数组的提问

Post by Yakshongas » 06 Jun 2020, 13:53

You're welcome @garry
Please mark your topics as solved if you don't need any further help. ✅

Need a little more help? Discord : Yakshongas#9893 🕹

feiyue
Posts: 230
Joined: 08 Aug 2014, 04:08

Re: 随机不重复数组的提问

Post by feiyue » 06 Jun 2020, 18:35

有位网友提出一种交换数组的方法来提取随机项目也很不错。

Code: Select all


min:=1, max:=230000, num:=160000

F3::
SetBatchLines, % (-1, bch:=A_BatchLines)

t:=A_TickCount
s:="", i:=min
loop, % max-min+1
  s.=(i++) ","
s:=SubStr(s, 1, -1)
Sort, s, Random D,
a:=StrSplit(s, ",")
msgbox, % A_TickCount-t

t:=A_TickCount
a:=[], i:=min
loop, % all:=max-min+1
  a[A_Index]:=i++
Loop, % num
{
  Random, k, (i:=A_Index), all
  v:=a[k], a[k]:=a[i], a[i]:=v
}
msgbox, % A_TickCount-t

SetBatchLines, %bch%
return


Post Reply

Return to “请求帮助”