Get Random From List but All Items also Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
VKG
Posts: 34
Joined: 23 Oct 2022, 05:00

Get Random From List but All Items also

Post by VKG » 10 Jun 2023, 04:52

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.
Last edited by VKG on 10 Jun 2023, 05:09, edited 1 time in total.

ntepa
Posts: 428
Joined: 19 Oct 2022, 20:52

Re: Get Random From List but All Items also

Post by ntepa » 10 Jun 2023, 05:08

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
}

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Get Random From List but All Items also

Post by boiler » 10 Jun 2023, 05:11

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))

VKG
Posts: 34
Joined: 23 Oct 2022, 05:00

Re: Get Random From List but All Items also

Post by VKG » 10 Jun 2023, 05:15

This shows this error:

Call to nonexistent function.
Specifically: Random(1, items.Length)

ntepa wrote:
10 Jun 2023, 05:08

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
}

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Get Random From List but All Items also

Post by boiler » 10 Jun 2023, 05:17

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.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Get Random From List but All Items also  Topic is solved

Post by boiler » 10 Jun 2023, 05:21

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)
}

VKG
Posts: 34
Joined: 23 Oct 2022, 05:00

Re: Get Random From List but All Items also

Post by VKG » 10 Jun 2023, 05:21

Sorry Bro for wrong forum. Kindly move it to v1.
boiler wrote:
10 Jun 2023, 05:17
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.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Get Random From List but All Items also

Post by boiler » 10 Jun 2023, 05:26

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.

VKG
Posts: 34
Joined: 23 Oct 2022, 05:00

Re: Get Random From List but All Items also

Post by VKG » 10 Jun 2023, 05:29

Thanks. I'll adopt this.
boiler wrote:
10 Jun 2023, 05:26
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.

Post Reply

Return to “Ask for Help (v1)”