Get Random From List but All Items also Topic is solved
Get Random From List but All Items also
I've a list like
Aa := "Word1"
Bb := "Word2"
Cc := "Word3"
Dd := "Word4"
Ee := "Word5"
I want to select every item from the list randomly one by one.
For example, if 'Bb' get selected for first time, then next time, a random item from remaining list- Aa Cc Dd Ee (i.e. excluding 'Bb') and
next time, it 'Dd' get selected for 2nd time, then next time, a random item from remaining list- Aa Cc Ee
and so on such that every item get selected. No item should left behind.
Means every item is to be selected but in random order.
Aa := "Word1"
Bb := "Word2"
Cc := "Word3"
Dd := "Word4"
Ee := "Word5"
I want to select every item from the list randomly one by one.
For example, if 'Bb' get selected for first time, then next time, a random item from remaining list- Aa Cc Dd Ee (i.e. excluding 'Bb') and
next time, it 'Dd' get selected for 2nd time, then next time, a random item from remaining list- Aa Cc Ee
and so on such that every item get selected. No item should left behind.
Means every item is to be selected but in random order.
Last edited by VKG on 10 Jun 2023, 05:09, edited 1 time in total.
Re: Get Random From List but All Items also
Code: Select all
list := "
(
Aa
Bb
Cc
Dd
Ee
)"
items := StrSplit(list, "`n")
loop items.Length {
randomIndex := Random(1, items.Length)
value := items.RemoveAt(randomIndex)
MsgBox "selected: " value
}
Re: Get Random From List but All Items also
Was just about to post this, so I will just to show a bit of an alternative:
Code: Select all
#Requires AutoHotkey v2.0
ListText := "
(
Aa
Bb
Cc
Dd
Ee
)"
ListArray := StrSplit(ListText, "`n", "`r")
while ListArray.Length
MsgBox ListArray.RemoveAt(Random(1, ListArray.Length))
Re: Get Random From List but All Items also
This shows this error:
Call to nonexistent function.
Specifically: Random(1, items.Length)
Call to nonexistent function.
Specifically: Random(1, items.Length)
ntepa wrote: ↑10 Jun 2023, 05:08Code: Select all
list := " ( Aa Bb Cc Dd Ee )" items := StrSplit(list, "`n") loop items.Length { randomIndex := Random(1, items.Length) value := items.RemoveAt(randomIndex) MsgBox "selected: " value }
Re: Get Random From List but All Items also
You must be running AHK v1. You posted in the v2 forum. There is now a separate section for v1. I will move the thread.
Also, by editing your post after receiving replies like you did, it looks like we didn't understand that your list were variables containing other text. Please don't do that and change the context of what has already been posted.
Also, by editing your post after receiving replies like you did, it looks like we didn't understand that your list were variables containing other text. Please don't do that and change the context of what has already been posted.
Re: Get Random From List but All Items also Topic is solved
Code: Select all
#Requires AutoHotkey v1.1.29
ListText := "
(
Word1
Word2
Word3
Word4
Word5
)"
ListArray := StrSplit(ListText, "`n", "`r")
while ListArray.Count() {
Random, Rand, 1, ListArray.Count()
MsgBox % ListArray.RemoveAt(Rand)
}
Re: Get Random From List but All Items also
Sorry Bro for wrong forum. Kindly move it to v1.
boiler wrote: ↑10 Jun 2023, 05:17You must be running AHK v1. You posted in the v2 forum. There is now a separate section for v1. I will move the thread.
Also, by editing your post after receiving replies like you did, it looks like we didn't understand that your list were variables containing other text. Please don't do that and change the context of what has already been posted.
Re: Get Random From List but All Items also
Using separate, individually named variables that contain values that are meant to be operated on as a list together (as you edited your post to show) is not a good approach. You should either have them in a string separated by some delimiter like we showed or in an array like we converted that string into.
Re: Get Random From List but All Items also
Thanks. I'll adopt this.
boiler wrote: ↑10 Jun 2023, 05:26Using separate, individually named variables that contain values that are meant to be operated on as a list together (as you edited your post to show) is not a good approach. You should either have them in a string separated by some delimiter like we showed or in an array like we converted that string into.